如何从文件中导入csv数据

17 浏览
0 Comments

如何从文件中导入csv数据

这个问题已经有了答案:

在Python中查找一个带有.txt扩展名的目录下的所有文件

在一个目录里,我有大约100个文件- csv数据。如何将它们导入到Python中?我进行了搜索并找到了:

import csv
f = open("imgdata.csv")
r = csv.reader(f)
f.close()

但它对于一个目录不起作用。这与查找文件无关,而是与导入它们有关。

admin 更改状态以发布 2023年5月20日
0
0 Comments

试试这个方法:

import os
directory = os.path.join("c:\\","path")
for root,dirs,files in os.walk(directory):
    for file in files:
       if file.endswith(".csv"):
           f=open(file, 'r')
               #  perform calculation
           f.close()

0
0 Comments

使用glob

import glob
import csv
for f_name in glob.glob("*.csv"):
    with open(f_name) as f:
        reader = csv.reader(f)
        # do stuff here

0