【问题标题】:Python 3.6 - How to pass file names into unique variablesPython 3.6 - 如何将文件名传递给唯一变量
【发布时间】:2018-01-13 09:34:49
【问题描述】:

我想为目录中的每个文件分配唯一的变量名。我不知道如何做到这一点。我是 python 新手,所以很抱歉代码很邋遢。

def DataFinder(path, extension):
    import os
    count = 0
    extensions = ['.txt','.csv','.xls','xlsm','xlsx']
    allfiles = []

    if not extension in extensions:
        print('Can\'t read data from this file type.\n','Allowed file types are\n',str(extensions))
    else:
        #loop through the files
        for root, dirs, files in os.walk(path):
            for file in files:
                #check if the file ends with the extension
                if file.endswith(extension):
                    count+=1
                    print(str(count)+': '+file)
                    allfiles.append(file)

        if count==0:
            print('There are no files with',extension,'extension in this folder.')
    return allfiles

如何修改此代码以将变量名称(如 df_number.of.file)分配给每次迭代作为字符串?

谢谢

我的最终目标是为每个文件以唯一的变量名称设置一组 DataFrame 对象,而无需手动创建这些变量。

建议的副本没有回答我的问题,也不适合我。

allfiles = {}
        #filter through required data extensions
        if not extension in extensions:
            print('Can\'t read data from this file type.\n','Allowed file types are\n',str(extensions))
        else:
            #loop through the files
            for root, dirs, files in os.walk(path):
                for file in files:
                    #check if the file ends with the extension
                    if file.endswith(extension):
                        #raise counter
                        count+=1
                        print(str(count)+': '+file)
                        allfiles.update({'df'+str(count) : path+file})

按照建议调整代码后,我的输出是字典:

{'df1': 'C:/Users/Bartek/Downloads/First.csv', 'df2': 'C:/Users/Bartek/Downloads/Second.csv', 'df3': 'C:/用户/Bartek/Downloads/Third.csv'}

我之前使用列表实现了类似的事情:

['df_1First.csv', 'df_2Second.csv', 'df_3Third.csv']

但我的确切问题是如何做到这一点:

对于字典中的每个对象: - 创建一个具有连续对象编号的变量

所以这个变量可以作为数据参数传递给 pandas.DataFrame()

我知道这是一个非常糟糕的主意 (http://stupidpythonideas.blogspot.co.uk/2013/05/why-you-dont-want-to-dynamically-create.html),所以你能告诉我使用 dict 的正确方法吗?

非常感谢

【问题讨论】:

  • 不要这样做——根据值设置值的名称是不好的编程习惯。相反,请使用字典,其中所需的名称是键。
  • @RoryDaulton 谢谢。请问我该如何提出正确的问题以找到有关为此目的使用字典的答案?

标签: python python-3.6


【解决方案1】:

你可以像这样修改第一个脚本。

从时间导入 gmtime, strftime

导入操作系统

def DataFinder(路径,扩展名):

count = 0
extensions = ['.txt','.csv','.xls','xlsm','xlsx']
allfiles = []

if not extension in extensions:
    print('Can\'t read data from this file type.\n','Allowed file types are\n',str(extensions))
else:
    #loop through the files
    for root, dirs, files in os.walk(path):
        for file in files:
            #check if the file ends with the extension
            if file.endswith(extension):
                count+=1
                #taking date and time
                date_time=strftime("%Y-%m-%d %H:%M:%S", gmtime())
                #now to get file name we are splite with (.)dot so in list we get first (i.e.file_name[0]) file name and (i.e.file_name[1]) as extension.
                file_name=file.split('.')
                allfiles.append(file_name[0]+date_time+'.'+file_name[1])

    if count==0:
        print('There are no files with',extension,'extension in this folder.')
return allfiles

打印 DataFinder('/home/user/tmp/test','.csv')

【讨论】:

    【解决方案2】:

    您应该能够修改这部分代码以完成您想要的。而不是打印出文件的数量。使用count 创建新的唯一文件名。

    if file.endswith(extension):
      count+=1
      newfile = ('df_' + str(count) + file)
      allfiles.append(newfile)
    

    count 对于每个不同的文件扩展名都是唯一的。您应该可以在allfiles 中找到新创建的文件名。

    编辑以使用 字典(感谢 Rory):我建议另一种路线。创建一个字典并使用文件名作为键。

    allfilesdict = {}
    ...
    if file.endswith(extension):
      count+=1
      newfile = ('df_' + str(count) + file)
      allfilesdict[file] = newfile
    

    如果您要在函数之外的某个地方使用它,请记住返回 allfilesdict

    【讨论】:

    • 谢谢 OLIVER.KOO。如果我在这里混淆了一些东西,我很抱歉。我正在从此代码中调用该函数: path = input('文件夹路径是什么?\n') ext = input('文件扩展名是什么?(即 .xls)\n') import DataProject as dp p = dp.FileHandler.DataFinder(path,ext) print(p) 理想情况下,我想为找到的每个文件返回带有字符串的唯一变量,这些文件可以传递给 pd.read_excel、read_csv 等。
    • 您是否要为每个文件创建df_numberOfFiles 字符串,扩展名重要吗?您是否将df_numberOfFiles 与文件名连接起来?能举个简单的例子吗?
    • 我想我想用 Rory 解释的坏方法来处理它。
    • Rory 建议使用我在编辑中包含的dictionary。修改您的 DataFinder 函数以执行此操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 2020-11-20
    • 2023-02-01
    • 1970-01-01
    • 2021-07-03
    • 2010-11-18
    • 2015-05-21
    相关资源
    最近更新 更多