如何在python中的文件夹中读取某些csv文件 [英] How to read some csv files in a folder in python

查看:1271
本文介绍了如何在python中的文件夹中读取某些csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有40个csv文件; file1.csv,file2.csv. ...,名为pathImage的文件夹中的file40.csv.我想阅读它们,然后将它们依次放入另一个名为"output.csv"的csv文件中.以下代码对我不起作用.

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)

我已经阅读了前面的问题,但是我不知道如何将它们放入另一个csv文件中?

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

感谢您的帮助

推荐答案

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')

这篇关于如何在python中的文件夹中读取某些csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆