【问题标题】:Reading multiple csv files with specific string value读取具有特定字符串值的多个 csv 文件
【发布时间】:2022-01-17 13:31:26
【问题描述】:

我的目录中有多个 csv 文件,但我想读取文件名中包含特定字符串的文件。

文件:

QA Finance GRM CONS ASPAC_Sales_6698_WI3_2021_ListPrice.csv,
QA Finance GRM CONS ASPAC_Sales_6698_WI4_2021_GrsToNet.csv,
QA Finance GRM CONS ASPAC_Sales_6698_WI3_2021_UnitsChanges.csv

我只想一次性读取具有“标价”和“单位更改”的文件。

试过这个:

os.chdir(path=source_path)
all_csv_files = glob.glob("*.csv")
print(all_csv_files)

for file in all_csv_files:
    if ("ListPrice" in file):
        uploadfiles = [f for f in listdir(source_path)
        if isfile(join(source_path, f))]
            print("Upload files:", *uploadfiles, sep='\n')

【问题讨论】:

  • os.chdir(path=source_path) all_csv_files = glob.glob("*.csv") print(all_csv_files) for file in all_csv_files: if ("ListPrice" in file): uploadfiles = [f for f in listdir(source_path) if isfile(join(source_path, f))] print("上传文件:", *uploadfiles, sep='\n')
  • 忘记添加代码...添加在上面的评论中
  • edit 您的问题并添加您的格式化代码,切勿将代码添加为注释。另外:minimal reproducible example && How to Ask
  • 请不要在 cmets 中放置大量代码 - 将其放在您的问题中它所属的位置并且可以正确格式化。您还需要更清楚地知道要对匹配文件做什么。

标签: python csv


【解决方案1】:

您可以使用此代码读取您的特定文件,也可以一次修改所有文件名 list-

import pandas as pd
filesName = [
    "QA Finance GRM CONS ASPAC_Sales_6698_WI3_2021_ListPrice.csv",
    "QA Finance GRM CONS ASPAC_Sales_6698_WI4_2021_GrsToNet.csv",
    "QA Finance GRM CONS ASPAC_Sales_6698_WI3_2021_UnitsChanges.csv"
]

keywords = [
    "ListPrice",
    "UnitsChanges"
]

for names in filesName:
    for key in keywords:
        if key in names:
            df = pandas.read_csv(names)
            print(df)

【讨论】:

    【解决方案2】:

    如果这个问题只是过滤一个列表,StackOverflow上有很多与'[python] filter a list'相关的帖子,我建议你去看看。

    专门针对您的问题,如何对每种文件进行“glob-ing”并将它们组合起来:

    lp_files = glob.glob('*ListPrice.csv')
    uc_files = glob.glob('*UnitsChanges.csv')
    filtered = lp_files + uc_files
    

    我认为这非常清楚地向您和其他任何人展示了您想要/期望的内容。

    如果你仍然只想 glob 一次,过滤多个文件,我建议创建一个小过滤函数:

    def csv_filter(fname):
        if 'ListPrice' in fname:
            return True
        if 'UnitsChanges' in fname:
            return True
        # if 'SomeOtherText' in fname:
        #     return True
    
        return False
    

    您可以非常轻松地从该列表中添加和删除文件名;对于任何与您的过滤器不匹配的文件,只需留下最后的 return False

    您可以从单行列表理解中调用它:

    all_csv_files = glob.glob('*.csv')
    filtered = [x for x in all_csv_files if csv_filter(x)]
    

    这相当于您正在尝试的这个更传统的 for 循环:

    
    filtered = []
    for x in all_csv_files:
        if  csv_filter(x):
            filtered.append(x)
    
    # Now, do something with filtered CSVs
    # ...
    

    另外,这里可能还有其他问题,但我不确定:

    if ("ListPrice" in file):
        uploadfiles = [f for f in listdir(source_path)
        if isfile(join(source_path, f))]
            print("Upload files:", *uploadfiles, sep='\n')
    

    你的条件是如果file 匹配你的'ListPrice' 过滤器,做一些事情,但你没有对file 做任何事情。在source_path 中找到“ListPrice”文件可能有更广泛的含义,但我希望看到file 在该 if 语句中的某处使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-31
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      相关资源
      最近更新 更多