我一直在讨论这个问题(很可能很简单),但我的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 ...
如有任何建议,将不胜感激。多谢各位
在for循环的每次迭代中都覆盖output.csv。试着这样做
现在,您的数据帧将全部保存在output_x.csv格式中。即,如果找到三个文件,您将看到以下文件:output_1.csv、output_2.csv、output_3.csv
相关问题 更多 >
编程相关推荐