Hi,
I am a beginner at Ruby, but I am trying to convert a csv file into a
tsv file.
The data is stored in source.csv and I need to out put it to a file in
the same directory called source.tsv, I also need to cater to output to
.txt and .xml, but starting with tsv first.
Contents of source.csv
"Firstname","Lastname","Postcode"
"John","Smith","EC1"
"Paula","James","NE13"
Output to file source.tsv
Firstname Lastname Postcode
John Smith EC1
Paula James NE13
I have started with this:
filename = ARGV.first
output_file_format = ARGV.second
file = open(filename, "r+")
valid_file_formats = ["tsv"]
if output_file_format == "tsv"
File.open (filename, "r") do |f|
new_file = File.open("source.tsv", "w")
f.each_line do |line|
fields = line.split (""\n")
fields.each { |fd| fd = "\"#{fd}\""
new_file.write(fields.join(",")
end
new_file.close
end
I understand what I need to do in my head, for each line remove the " or
, characters and replace the , character with a tab (\t), but I am
struggling with the syntax.
Using a bit of help from http://www.ruby-forum.com/topic/4097630#new
Thanks in advance
···
--
Posted via http://www.ruby-forum.com/.