How to read some csv files in a folder in python

社会主义新天地 提交于 2019-12-13 02:39:33

问题


There are 40 csv files; file1.csv, file2.csv. ..., file40.csv in a folder called pathImage. I want to read them, and put them in another csv file sequentially that we called 'output.csv'. The following code does not work for me.

im_list=[]
for i in pathImage:
    f= open(i, 'rb') 
    reader = csv.reader(f, delimiter=',')
    header = reader.next()
    zipped = zip(*reader)
    for j in zipped[0]:       #zipped[0] is the name of images
        im_r=misc.imread("%s.png" %j)
        im_list.append(im_r)

I already read the previous questions, but I do not know how can I put them into another csv file?

I appreciate any help


回答1:


import pandas as pd
df = pd.concat([pd.read_csv('file%d.csv' % x) for x in range(1,41)])
df.to_csv('output.csv')


来源:https://stackoverflow.com/questions/31620829/how-to-read-some-csv-files-in-a-folder-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!