It is well known that the method of inputting data to the Rails database is to create a yaml file for fixtures and input it with the rails command. However, in reality, it seems that the original data is often prepared in Excel etc. and handled in CSV etc. I also tried yaml converter etc. but it didn't work to put the fixtures key, so I made it myself. I haven't done anything like processing escape characters, so if the original data deals with such things, please process it appropriately.
For example, suppose you have a CSV file like this with the column name on the first line.
id,item,price
1, apple, 100
2, mandarin oranges, 80
3, banana, 60
4, Melon, 2000
5, strawberry, 300
You will need such a yaml file to handle with Fixtures.
data1:
id: 1
item: apples
price: 100
data2:
id: 2
item: Mandarin orange
price: 80
data3:
id: 3
item: banana
price: 60
data4:
id: 4
item: Melon
price: 2000
data5:
id: 5
item: strawberry
price: 300
The place of data1:
is the fixtures key, which is not reflected in the database, but it seems that you have to give a unique value as a hash for each record.
The script looks like this. After converting from Excel to CSV, UTF-8 has a BOM, so there is an option when opening the file.
!/use/bin/env ruby
filename = ARGV[0]
# Option to erase UTF-8 BOM
file = open(filename, 'r:BOM|UTF-8')
# Get the first line of the CSV file
column = file.gets.chomp.split(",")
# The fixtures key prefix (data) followed by the counter (num)
prefix = "data"
num = 1
str = ""
file.each do |line|
unless line.strip == "" ## Ignore blank lines
str += "#{prefix}#{num}:\n"
(0 .. column.length - 1).each do |i|
str += " #{column[i]}: #{line.chomp.split(",")[i]}\n"
end
str += "\n"
num += 1
end
end
print str
It looks like this when running from Shell.
$ ./csv2yaml.rb fruits.csv > fruits.yml
There was no problem converting CSV data of about 500 lines, and it seems to be okay because it was imported by fixtures. I was able to convert multiple CSV files with different columns.
It may be easier to make if you use yaml library etc.
Recommended Posts