简体   繁体   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)

The output prints the lots of names in the console.输出会在控制台中打印大量名称。 But In excel, It stores only the least one.但是在excel中,它只存储最少的一个。 How do i store all the names in the excel.如何将所有名称存储在excel中。

To get on clear idea:要获得清晰的想法:

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

In excel i see the only carwl xyz.在excel中,我看到了唯一的carwl xyz。

You are overwriting your excel file in each iteration of the loop, so try to collect all your data first, then generate a single DataFrame based on collected data and save it as Excel您在循环的每次迭代中都覆盖了您的 excel 文件,因此请尝试先收集所有数据,然后根据收集的数据生成单个 DataFrame 并将其保存为 Excel

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

PS you should really provide a sample and desired data sets if you want to have neat answers... PS,如果您想得到简洁的答案,您应该真正提供示例和所需的数据集...

暂无
暂无

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

相关问题 Python Excel 在现有工作表中写入 DataFrame - Python Excel write a DataFrame in an existing sheet 如何将 python output 写入 excel 表? - How to write python output to an excel sheet? 使用 Pandas 将 Dataframe 行写入 excel 表 - Write Dataframe row to excel sheet using Pandas 使用Python编写控制台输出(用空格分隔的文本)以分隔Excel工作表的列 - Writing console output(space seperated text) to seperate columns of excel sheet using Python Python:将数据框写入到现有的Excel中,其中包含带有图像的工作表 - Python: Write a dataframe to an already existing excel which contains a sheet with images "使用 python pandas 将现有的 excel 表附加到新的数据框" - Append existing excel sheet with new dataframe using python pandas 想要读取一个文件并使用 python 写入现有的 excel 表 - Want to read an a file and write to existing excel sheet using python 使用 pandas 和 output 将 Excel 表中的列取消隐藏到 Z6A8064B5DF4794555700553 - Unhide columns in an Excel sheet with pandas and output into a dataframe 使用 python 将 SQL 输出数据更新到相应工作表中的现有 Excel - Update SQL output data into Existing Excel in respective sheet using python 如何分组并获取 Dataframe 中每个人的唯一值计数,然后将其写入 Python 中的 Excel 工作表? - How can I groupby and get unique value counts for each person in Dataframe and then write that to an Excel sheet in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM