writing csv into file

A

Alfred Pier

Hello guys ! I'm new in Ruby....Hope you can help because i'm trying a
lot and just can't see where is the problem..
I have this xml, and not only this, i want just for any xml file that i
might put :

<?xml version="1.0"?>
<records>
<company>
<id>p1</id>
<name>Skoda</name>
<price>10000</price>
<stock>4</stock>
<country>Czech Republic</country>
</company>
<company>
<id>p2</id>
<name>Mercedes</name>
<price>50000</price>
<stock>5</stock>
<country>Germany</country>
</company>
<company>
<id>p3</id>
<name>Alfa Romeo</name>
<price>18000</price>
<stock>19</stock>
<country>Italy</country>
</company>
<company>
<id>p4</id>
<name>Fiat</name>
<price>1500</price>
<stock>15000</stock>
<country>Italy</country>
</company>
</records>


AND USING THE CODE BELOW:

require 'nokogiri'
require 'pp'

xmldoc = Nokogiri::XML(IO.read("xmlinputfile.xml"))
columns = xmldoc.xpath('/*/*[position()=1]/*').map(&:name)

print "give csv output file\n"
csvoutputfile = gets.chomp
File.open(csvoutputfile, 'w') do |f|
rows =[]
xmldoc.xpath('/*/*').each do |row_xml|
row_values = []
f.puts " #{rows << row_values}"
row_xml.xpath('./*').each do |field|
f.puts "#{row_values << field.text}"

end
end
pp rows


end




I WANT TO CONVERT IT TO CSV FORMAT LIKE THIS:

id,name,price,stock,country
p1,Skoda,10000,4,Czech Republic,
p2,Mercedes,50000,5,Germany,
 
R

Robert Klemme

Hello guys ! I'm new in Ruby....Hope you can help because i'm trying a
lot and just can't see where is the problem..
I have this xml, and not only this, i want just for any xml file that i
might put :

<?xml version=3D"1.0"?>
<records>
=A0 <company>
=A0 =A0 <id>p1</id>
=A0 =A0 <name>Skoda</name>
=A0 =A0 <price>10000</price>
=A0 =A0 <stock>4</stock>
=A0 =A0 <country>Czech Republic</country>
=A0</company>
=A0<company>
=A0 =A0 <id>p2</id>
=A0 =A0 <name>Mercedes</name>
=A0 =A0 <price>50000</price>
=A0 =A0 <stock>5</stock>
=A0 =A0 <country>Germany</country>
=A0</company>
=A0<company>
=A0 =A0 <id>p3</id>
=A0 =A0 <name>Alfa Romeo</name>
=A0 =A0 <price>18000</price>
=A0 =A0 <stock>19</stock>
=A0 =A0 <country>Italy</country>
=A0</company>
=A0<company>
=A0 =A0 <id>p4</id>
=A0 =A0 <name>Fiat</name>
=A0 =A0 <price>1500</price>
=A0 =A0 <stock>15000</stock>
=A0 =A0 <country>Italy</country>
=A0</company>
</records>


AND USING THE CODE BELOW:

require 'nokogiri'
require 'pp'

xmldoc =3D Nokogiri::XML(IO.read("xmlinputfile.xml"))
columns =3D xmldoc.xpath('/*/*[position()=3D1]/*').map(&:name)

IMHO this is sufficient as XPath: '/*/*[1]/*'
print "give csv output file\n"
csvoutputfile =3D gets.chomp
File.open(csvoutputfile, 'w') do |f|
=A0 rows =3D[]
=A0 xmldoc.xpath('/*/*').each do |row_xml|
=A0 =A0 =A0 =A0 =A0row_values =3D []
=A0 =A0 =A0 =A0 =A0f.puts " =A0#{rows << row_values}"

You should separate filling your data structure from doing CSV output
above. Right now you are printing rows for every record!
=A0 =A0 =A0 =A0 =A0row_xml.xpath('./*').each do |field|
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0f.puts "#{row_values << field.text}"

Again, you should separate filling your data structure from doing CSV
output above (or completely get rid of "rows") e.g.

puts row_xml.xpath('./*').map(&:text).join
=A0 =A0 =A0 =A0 =A0end
=A0 end
=A0 pp rows

You are seeing output of that last line.

I would also consider using CSV for writing because that will give you
proper quoting

CSV.open("foo.txt", "w", col_sep:",") do |csv|
...
csv << row_xml.xpath('./*').map(&:text)
...
end
=A0I WANT TO CONVERT IT TO CSV FORMAT LIKE THIS:

id,name,price,stock,country
p1,Skoda,10000,4,Czech Republic,
p2,Mercedes,50000,5,Germany,
.
.
.
. and so on



THE RUBY CODE OF CONVERTION WORKS FINE BUT ONLY WHEN I RUN IT IN THE
COMMAND PROMT, ONLY THERE IS SHOWS THE CSV FORMAT RIGHT

BUT I WANT TO WRITE IT IN A TXT FILE AND THIS IS WHAT I GET WHEN I TRY
WRITING IT INTO A txt :

Do you use output redirection for your script?
=A0 =A0[[]]

["p1"]
["p1", "Skoda"]
["p1", "Skoda", "10000"]
["p1", "Skoda", "10000", "4"]
["p1", "Skoda", "10000", "4", "Czech Republic"]
[["p1", "Skoda", "10000", "4", "Czech Republic"], []]
["p2"]
["p2", "Mercedes"]
["p2", "Mercedes", "50000"]
["p2", "Mercedes", "50000", "5"]
["p2", "Mercedes", "50000", "5", "Germany"]

[["p1", "Skoda", "10000", "4", "Czech Republic"], ["p2", "Mercedes",
"50000", "5", "Germany"], []]
["p3"]
["p3", "Alfa Romeo"]
["p3", "Alfa Romeo", "18000"]
["p3", "Alfa Romeo", "18000", "19"]
["p3", "Alfa Romeo", "18000", "19", "Italy"]
[["p1", "Skoda", "10000", "4", "Czech Republic"], ["p2", "Mercedes",
"50000", "5", "Germany"],
["p3", "Alfa =A0 =A0 Romeo", "18000", "19", "Italy"],[]]



PLEASE CAN ANYBODY SHOW ME MY MISTAKE AND WHAT SHOULD I DO BECAUSE I
CANNOT FIND IT... AND I
DON'T UNDERSTAND THE REASON THAT IT SHOWS IT LIKE THIS IN THE txt FILE.

THANK YOU IN ADVANCE !!

Please do NOT shout.

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,439
Members
47,101
Latest member
DoloresHol

Latest Threads

Top