Ruby:将大的.xml文件转换为csv
我有个小问题。 我需要将大型xml文件(每个1-4GB)转换为CSV。 我知道我可以用Nokogiri的SAX解析器来实现这一点,但我被卡住了Ruby:将大的.xml文件转换为csv,ruby,xml,csv,nokogiri,Ruby,Xml,Csv,Nokogiri,我有个小问题。 我需要将大型xml文件(每个1-4GB)转换为CSV。 我知道我可以用Nokogiri的SAX解析器来实现这一点,但我被卡住了 <Documents> <Document DocID="10170306" DocType="Message"> <FieldValues> <E03>-1166737392</E03> <E05>petrosky (petrosk
<Documents>
<Document DocID="10170306" DocType="Message">
<FieldValues>
<E03>-1166737392</E03>
<E05>petrosky (petrosky@foo.com)</E05>
<E06>00000000B89476181EE6C34FB4E9D87F9E44A85944002000</E06>
<E07>\foo-dedup-global.mbox_99.mbox\</E07>
<E08>5/12/2011 6:32:38 PM</E08>
<E09>Fwd: important decision for v1 launch</E09>
<E10>Susan Infantino (susani@foo.com); Mike Yang (foo@mail.com)</E10>
<F01>Jun 8 2011 7:43AM</F01>
<F02>May 12 2011 6:32PM</F02>
<F03>Msg0002_important decisi.html</F03>
<F04>MSMAIL</F04>
<F05>CA4DBB95C638FB656CB02627DDEA90C9</F05>
<F06>28677</F06>
<F07>foo-dedup-global.mbox_99.mbox.pst</F07>
<F08>10164846</F08>
<F09>10170306</F09>
<E11>0</E11>
<E12><BANLkTi=yztN5Pd0v9i9+zN=aYhAo5Y8ffA@mail.foo.com></E12>
</FieldValues>
<Files>
<File FileType="Native">
<ExternalFile FilePath="\04_EXT\31\foo-dedup-global.mbox_99.mbox.pst10164846.dir\foo-dedup-global.mbox_99.mbox\" FileName="Msg0002_important decisi.html" FileSize="28677" Hash="CA4DBB95C638FB656CB02627DDEA90C9" HashType="MD5" />
</File>
</Files>
<Locations>
<Location>
<Custodian>Yang_Mike</Custodian>
<LocationURI>\\ANNATXCIFS02\PN_Dunbar_F01401\04_EXT\31\foo-dedup-global.mbox_99.mbox.pst10164846.dir\foo-dedup-global.mbox_99.mbox\Msg0002_important decisi.html</LocationURI>
</Location>
</Locations>
</Document>
</Documents>
-1166737392
彼得罗斯基(petrosky@foo.com)
00000000 B89476181EE6C34FB4E9D87F9E44A85944002000
\foo-dedup-global.mbox_99.mbox\
2011年5月12日下午6:32:38
Fwd:v1发射的重要决策
苏珊:不(susani@foo.com); 杨迈克(foo@mail.com)
2011年6月8日上午7:43
2011年5月12日下午6:32
msg002_重要决策.html
MSMAIL
CA4DB95C638FB656CB02627DDEA90C9
28677
foo-dedup-global.mbox_99.mbox.pst
10164846
10170306
0
BANLkTi=yztN5Pd0v9i9+zN=aYhAo5Y8ffA@mail.foo.com
杨宇迈克
\\ANNATXCIFS02\PN_Dunbar\u F01401\04_EXT\31\foo dedup global.mbox_99.mbox.pst1064846.dir\foo dedup global.mbox_99.mbox\msg002\u important decisi.html
我在事件驱动编程方面做了一些尝试
require 'fileutils'
require 'faster_csv'
require 'nokogiri'
file = ARGV[0]
include Nokogiri
class Xmlfile < XML::SAX::Document
def start_element name, attrs
# Process data here
if name == 'Document'
documentName = [*attrs]
puts documentName
end
if name == 'File'
file = [*attrs]
puts file
end
if name == 'ExternalFile'
externalFile = [*attrs]
puts externalFile
end
end
# def end_element name, attrs
# end
end
parser = XML::SAX::Parser.new(Xmlfile.new)
parser.parse_file(file)
需要“fileutils”
需要“更快的csv”
需要“nokogiri”
文件=ARGV[0]
包括Nokogiri
类Xmlfile
看起来您正在输出puts
中所需的信息,并试图将数据捕获到变量中。您缺少使用变量将数据发送到CSV生成器的部分
您可以将documentName
、file
和externalFile
附加到数组中,并将其传递给CSV
显示了几种生成输出的好方法。查看“写作”部分了解更多信息
从文档中:
FasterCSV.open("path/to/file.csv", "w") do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["another", "row"]
# ...
end
FasterCSV.open(“path/to/file.csv”,“w”)do|csv|
csv和问题到底是什么?上面的代码起作用,即它输出所有文档
、文件
和外部文件
节点的所有属性。@undur\u gongor他想将其转换为CSV,在标题中。请附上如何将部分xml文件映射到csv列。以下是csv的标准库文档:如何使用Ruby将xml动态添加到csv?我已经能够写入CSV,但它只会在一行中输入数据-因此,当它迭代时,它会不断被覆盖。你需要以新问题的形式提问,而不是作为对答案的评论。我们对你的情况知之甚少,无法回答你。