【问题标题】:Python loop only printing final loop of the dataframe as output to ExcelPython循环仅将数据帧的最终循环打印为Excel的输出
【发布时间】:2020-08-05 17:23:30
【问题描述】:

一直停留在这个(很可能非常简单)问题上,但我对 Excel 的输出只打印了最终循环中的值。我相信这与缩进有关,并尝试将值排除在失败的 for 循环之外。以下是一些有助于理解问题的虚拟代码:

#dummy code

#reads each file in folder, runs through a bunch of functions, and prints output in console
import os,glob
import csv
import pandas as pd
filename ='path to folder'
for filename in glob.glob(os.path.join(folder_path, '*.txt')):
    with open(filename, 'r') as f:
        text = f.read()
        data =function1(file= filename)
        data= function2(file= filename)
        data = function3(file= filename)
        final =pd.DataFrame(data)
        print(final)
        final.to_excel('output.xlsx')

控制台中的输出看起来像这样,这是正确的,我想导出到 csv:

0  some text here ...
1  more text...
2  clear text...
3  final data...
                                                   0
0  yes no...
1  does lots...
2  happy sunflower ...
3  ate food...

                                                  0
0  final data ...
1  apple strawberry...
2  different dataset...
3  dinne meals ...

任何建议将不胜感激。谢谢。

【问题讨论】:

标签: python excel pandas for-loop


【解决方案1】:

您在 for 循环的每次迭代中都覆盖了 output.csv。尝试这样做

import pandas as pd
#use enumerate to have a counter variable at each index
for filename, n in enumerate(glob.glob(os.path.join(folder_path, '*.txt'))):
    with open(filename, 'r') as f:
        #your logic
        #this will be overwritten on next pass of for loop
        final = pd.DataFrame(data)
        #write to file with appended index- avoids file being overwritten on next pass
        final.to_csv("output_%d.csv" % n)

现在您的数据框将全部以 output_x.csv 的形式保存。即如果找到三个文件,您将看到以下文件:output_1.csv、output_2.csv、output_3.csv

【讨论】:

    猜你喜欢
    • 2021-04-30
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多