简体   繁体   English

python 打开不同文件夹中的 csv 文件

[英]python open csv files in different folders

I want to read and assign csv files into variables.我想读取 csv 文件并将其分配到变量中。 But csv files are in different folders.但是 csv 文件位于不同的文件夹中。

Here's the thing事情是这样的

1. /current/a/1/op/one.csv
2. /current/a/1/no/two.csv
3. /current/a/2/op/three.csv
4. /current/b/1/op/four.csv

so I want to assign csv files in /op folders to grape by stacking所以我想通过堆叠将/op文件夹中的 csv 文件分配给grape

and I want to assign csv files in /no folders to apple我想将/no文件夹中的 csv 文件分配给apple

this is my code这是我的代码

grape1 = pd.read_csv('current/a/1/op/one.csv')
grape2 =pd.read_csv('current/a/2/op/three.csv')
grape3 = pd.read_csv('current/b/1/op/four.csv')

apple = pd.read_csv('current/a/1/no/two.csv')

grape= grape1+grape2+grape3

but I have many files and folders, so my code will be too long.但是我有很多文件和文件夹,所以我的代码会太长。

how can I solve it?我该如何解决?

You could use a loop你可以使用循环

import glob
import panda as pd
df_list = []
for f in glob.glob('current/*/*/op/*.csv'):
    df_list.append(pd.read_csv)
grape = pd.concat(df_list)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 根据源文件夹将所有 csv 文件从不同的源文件夹复制到目标? - how to copy all csv files from different source folders into target as per source folders using python? 将不同文件夹中的多个 csv 文件连接到 python 中的一个 csv 文件中 - Concatenate multiple csv files from different folders into one csv file in python Python将文件保存在不同的文件夹中 - Python Saving files in different folders Python:循环在python中打开多个文件夹和文件 - Python: Loop to open multiple folders and files in python 使用 Python 将编写的代码应用于不同文件夹中的所有 CSV 文件 - Apply a written code to all CSV files across different folders using Python 如何在 python 中以 dataframe 格式一次打开位于不同子文件夹中的多个 excel 压缩文件(.gz 文件)? - How to open multiple zipped excel files(.gz file) located in different sub folders at once in dataframe format in python? 如何打开Python中的csv文件 - How to open .csv files in Python 在Python中输入不同文件夹中的文件作为ffmpeg的参数 - Inputting files in different folders as arguments to ffmpeg in Python python如何访问不同文件夹中的所有文件 - python how to access all the files that are in different folders Python 列出不同文件夹中的某些文件 - Python list certain files in different folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM