I would like to make a script for reading all these files and converting
them to CSV files separated by commas and I would like all fields to be
quoted. If possible change encoding to UTF8 without BOM.
new_file = FasterCSV.new()
array_of_arrays.each do | item |
聽聽new_file.add_row(item)
end
File.open("saved_file.csv", "w") { |f| new_file.dump }
It seems to me that there must be a better way to do what you're trying
to accomplish. I don't know why you'd want to mess-around with csv
files in the first place. I don't understand why the comma delimited
csv file is better than the original. You could just use the original.
Also, if you're just doing a simple conversion, I'd just use simple ruby
code instead of having to learn everything about the CSV stuff:
File.open("original_file.txt", "r") do |f|
聽聽new_file = File.open("new_file.csv", "w")
聽聽f.each_line do |line|
聽聽聽聽fields = line.split(";")
聽聽聽聽fields.each { |fd| fd = "\"#{fd}\""
聽聽聽聽new_file.write(fields.join(",")
聽聽end
聽聽new_file.close
end
Perhaps if you explain what you're trying to do with these files, I
could give you better advise. Ruby has many tools to save objects like
YAML and JSON. Generally its better not to mess around with formatting
files yourself.
new_file = FasterCSV.new()
array_of_arrays.each do | item |
new_file.add_row(item)
end
File.open("saved_file.csv", "w") { |f| new_file.dump }
It seems to me that there must be a better way to do what you're trying
to accomplish. I don't know why you'd want to mess-around with csv
files in the first place. I don't understand why the comma delimited
csv file is better than the original. You could just use the original.
Also, if you're just doing a simple conversion, I'd just use simple ruby
code instead of having to learn everything about the CSV stuff:
File.open("original_file.txt", "r") do |f|
new_file = File.open("new_file.csv", "w")
f.each_line do |line|
fields = line.split(";")
fields.each { |fd| fd = "\"#{fd}\""
new_file.write(fields.join(",")
end
new_file.close
end
Perhaps if you explain what you're trying to do with these files, I
could give you better advise. Ruby has many tools to save objects like
YAML and JSON. Generally its better not to mess around with formatting
files yourself.
Hi!
The files are to be read by some system that cannot handle csv files
with semicolon. The problem is that the fields might contain commas
also. So the file should be comma separated with quoted fields.
I get these error messages with your last example:
If the file is large this can easily break because you need to read
the whole thing into memory. I'd also rather use the proper tool for
the job instead of cooking something with regexp. In this case I'd do
require 'csv'
CSV.open("new.csv", "wb", col_sep: ",", force_quotes: true) do |csv_out|
CSV.foreach("old.csv", col_sep: ";") do |rec|
csv_out << rec
end
end
Kind regards
robert
路路路
On Wed, Apr 18, 2012 at 10:30 PM, Jan E. <lists@ruby-forum.com> wrote:
cristian cristian wrote in post #1057250:
Ok, thanks! Will this save the new file with the name "new_csv"?
No, it will overwrite the original file. If you want to write the CSV to
a new file, change the code to
File.open 'new.csv', 'w' do |csv|
new_csv = File.read('old.csv').gsub(/[^\n\r;]+/, '"\0"').gsub(';',
',')
csv.print new_csv
end
If the file is large this can easily break because you need to read
the whole thing into memory.
I don't expect the CSVs to be *that* big. But sure, if we're talking
about hundreds of millions of entries here, you'll have to read the file
in small portions.
I'd also rather use the proper tool for
the job instead of cooking something with regexp.
Well, that's probably a question of personal preferences. I don't think
it's necessary to load a complete library for every tiny task that comes
around.
I mean: If I want to do some simple matrix calculations for example, I
don't really need a full 100 MB algebra library.
Of course you can write everything yourself. For any other than
trivial applications it's absurd though. Plus, even for the small
ones using a lib which exists vs. coding yourself is often quicker.
As always, it's a matter of tradeoffs.
Btw, your code creates two copies of the input. You could reduce
memory requirements by using String#gsub! instead of String#gsub.
Kind regards
robert
路路路
On Thu, Apr 19, 2012 at 12:19 AM, Jan E. <lists@ruby-forum.com> wrote:
Robert Klemme wrote in post #1057261:
I'd also rather use the proper tool for
the job instead of cooking something with regexp.
Well, that's probably a question of personal preferences. I don't think
it's necessary to load a complete library for every tiny task that comes
around.
I mean: If I want to do some simple matrix calculations for example, I
don't really need a full 100 MB algebra library.