简体   繁体   English

如何读取以 Python pandas 中的特定子字符串开头的 CSV 文件?

[英]How to read a CSV file that starts with a specific substring in Python pandas?

Say I have a CSV file whose name is like:假设我有一个 CSV 文件,其名称如下:

Pokémon_Pikachu.csv.

Is there a way to read it if I give only the first substring (Pokémon) and still read it in Pandas ?如果我只给出第一个子字符串(神奇宝贝)并且仍然在Pandas读取它,有没有办法读取它?

import glob
import pandas as pd

for file in glob.glob("Pokémon*.csv"):
    print (file)

this will get you the csv file names that start with Pokémon and if you want to read all the csv files into one,这将为您提供以 Pokémon 开头的 csv 文件名,如果您想将所有 csv 文件读入一个,

main_df = pd.DataFrame()
for file in glob.glob("Pokémon*.py"):
    df = pd.read_csv(file)
    if main_df.empty:
        main_df = df
    else:
        main_df = main_df.join(df, how='outer')

print main_df.head()

http://www.pythonforbeginners.com/code-snippets-source-code/python-os-listdir-and-endswith http://www.pythonforbeginners.com/code-snippets-source-code/python-os-listdir-and-endswith

you can use os.listdir() to get a list of the contents of a directory, then filter those with string.startswith(substring) or string.endswith(substring) .您可以使用os.listdir()获取目录内容的列表,然后使用string.startswith(substring)string.endswith(substring)过滤那些内容。

That would give you the filename(s) that you could put into pd.read_csv(filename)这将为您提供可以放入pd.read_csv(filename)

I am not sure what you mean but I suppose that you have files in directory with prefix Pokémon .我不确定你的意思,但我想你的目录中有前缀Pokémon文件。 The sollution is:解决办法是:

import pandas as pd
import os
import glob

for file in glob.glob(os.path.join(input_dir, 'Pokemon_*.csv')):
    pd.read_csv(file)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM