Python 3.6 - 如何将文件名传递给唯一变量
Python 3.6 - How to pass file names into unique variables
我想为目录中的每个文件分配唯一的变量名。我不知道如何做到这一点。我是 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:/Users/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 的正确方法吗?
非常感谢
您应该可以修改这部分代码来完成您想要的。而不是打印出文件的数量。使用 count
创建新的唯一文件名。
if file.endswith(extension):
count+=1
newfile = ('df_' + str(count) + file)
allfiles.append(newfile)
count
对于每个不同的文件扩展名都是唯一的。您应该能够在 allfiles
.
中找到新创建的文件名
编辑使用词典(感谢罗里):我会建议一个替代路线。创建字典并使用文件名作为键。
allfilesdict = {}
...
if file.endswith(extension):
count+=1
newfile = ('df_' + str(count) + file)
allfilesdict[file] = newfile
然后记得 return allfilesdict
如果你打算在你的函数之外的地方使用它。
您可以像这样修改第一个脚本。
从时间导入 gmtime、strftime
进口os
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')
我想为目录中的每个文件分配唯一的变量名。我不知道如何做到这一点。我是 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:/Users/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 的正确方法吗?
非常感谢
您应该可以修改这部分代码来完成您想要的。而不是打印出文件的数量。使用 count
创建新的唯一文件名。
if file.endswith(extension):
count+=1
newfile = ('df_' + str(count) + file)
allfiles.append(newfile)
count
对于每个不同的文件扩展名都是唯一的。您应该能够在 allfiles
.
编辑使用词典(感谢罗里):我会建议一个替代路线。创建字典并使用文件名作为键。
allfilesdict = {}
...
if file.endswith(extension):
count+=1
newfile = ('df_' + str(count) + file)
allfilesdict[file] = newfile
然后记得 return allfilesdict
如果你打算在你的函数之外的地方使用它。
您可以像这样修改第一个脚本。
从时间导入 gmtime、strftime
进口os
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')