将lxml解析的输出写入新文件

时间:2021-11-27 14:07:11

I have been receiving help on this forum to parse an xml file and pull out certain values. I can successfully print the required values to the screen, using the below:

我一直在这个论坛上收到帮助来解析一个xml文件并提取某些值。我可以使用以下内容成功将所需的值打印到屏幕:

for info in root.xpath('//xmlns:ProgramInformation', namespaces=nsmap):

   print (info.get('programId')) # retrieve crid
   print (info.find('.//xmlns:Title', namespaces=nsmap).text) # retrieve title
   print (info.find('.//xmlns:Genre/xmlns:Name', namespaces=nsmap).text) # retrieve genre

I now need to write the output to a file (not in XML format but in the format ABC|DEF|GHI, with each set on a new line).

我现在需要将输出写入文件(不是XML格式,而是格式为ABC | DEF | GHI,每一组都在新行上)。

I experimented with fo.write (which I have used elsewhere), but this doesn't appear to be the solution. I also looked at the element tree 'write' command, but I don't understand how to implement it.

我尝试了fo.write(我在其他地方使用过),但这似乎不是解决方案。我还查看了元素树'write'命令,但我不明白如何实现它。

Can someone advise how to construct the strings from the lxml output and write it to a file?

有人可以建议如何从lxml输出构造字符串并将其写入文件?

1 个解决方案

#1


0  

Open the output file using open with the write mode (w), then use file.write to write to the file.

使用open模式打开输出文件(w),然后使用file.write写入文件。

with open('output.txt', 'w') as f:
    for info in root.xpath('//xmlns:ProgramInformation', namespaces=nsmap):
       crid = (info.get('programId')) # retrieve crid
       title = (info.find('.//xmlns:Title', namespaces=nsmap).text) # retrieve title
       genre = (info.find('.//xmlns:Genre/xmlns:Name', namespaces=nsmap).text) # retrieve genre
       f.write('{}|{}|{}\n'.format(crid, title, genre))

#1


0  

Open the output file using open with the write mode (w), then use file.write to write to the file.

使用open模式打开输出文件(w),然后使用file.write写入文件。

with open('output.txt', 'w') as f:
    for info in root.xpath('//xmlns:ProgramInformation', namespaces=nsmap):
       crid = (info.get('programId')) # retrieve crid
       title = (info.find('.//xmlns:Title', namespaces=nsmap).text) # retrieve title
       genre = (info.find('.//xmlns:Genre/xmlns:Name', namespaces=nsmap).text) # retrieve genre
       f.write('{}|{}|{}\n'.format(crid, title, genre))