繁体   English   中英

使用DataFrame python将控制台输出写入excel表

[英]write console output to excel sheet using DataFrame python

for foo in root.finadall(xpath):
       print (foo.name[xpath], foo.country[xpath])
       df = DataFrame({'Name': foo.name[xpath], 'conutry': foo.country[xpath]})
       df.to_excel('foo.xlsx', sheet_name='sheet1', index=False)

输出会在控制台中打印大量名称。 但是在excel中,它只存储最少的一个。 如何将所有名称存储在excel中。

要获得清晰的想法:

The console output:
Name           Country 
jennfier       abc
andy john      cde
carwl          xyz

在excel中,我看到了唯一的carwl xyz。

您在循环的每次迭代中都覆盖了您的 excel 文件,因此请尝试先收集所有数据,然后根据收集的数据生成单个 DataFrame 并将其保存为 Excel

DataFrame({'Name': collected_data_list}).to_excel('foo.xlsx', sheet_name='sheet1', index=False)

PS,如果您想得到简洁的答案,您应该真正提供示例和所需的数据集...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM