【问题标题】:Read more csv files across different directories with python使用 python 跨不同目录读取更多 csv 文件
【发布时间】:2021-05-10 20:10:48
【问题描述】:

嘿,我想用 python 跨不同目录读取更多 csv 文件。现在我只能读取一个文件。我的 csv 文件有一个“,”而不是“。” (小数点)。例如,一个文件夹有十个子文件夹,每个子文件夹中有五个 csv 文件。我该怎么做? for 循环?

import numpy as np
with open('data.csv') as data:
    data = np.genfromtxt((line.replace(',', '.') for line in data), delimiter=";")

【问题讨论】:

  • 使用for path, dirs, files in os.walk(parent_directory)

标签: python csv


【解决方案1】:

可以使用os.walk(),例如:

import os
for root, dirs, files in os.walk(".", topdown=False):
   for name in files:
      print(os.path.join(root, name))
   for name in dirs:
      print(os.path.join(root, name))

【讨论】:

  • 在哪里“。”表示您的 python 文件所在的目录相同
  • @nico.wagner 如果这可行,您介意点击复选标记接受我的回答吗?
  • 是的,当然,抱歉。我读过这个,但不在我的电脑上。现在(早上)我会检查一下。 :-)
猜你喜欢
  • 2018-11-01
  • 2020-10-05
  • 2021-06-13
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 2018-12-21
  • 1970-01-01
相关资源
最近更新 更多