【发布时间】:2020-08-07 05:43:50
【问题描述】:
我需要从列表中读取文件。具体来说,我需要查找存储在特定目录中的文件。由于它可能不记得存储在那里的所有文件或只是正确的名称,我需要列出他们的。为了完成这一步,我发现了这篇有用的帖子:
How do I display the the index of a list element in Python?
现在我有一个如下列表:
file=[]
for f in glob.glob("*.txt"):
file.append(f)
for (num,item) in enumerate(file):
print(num+1,item) # updated after Thierry's comment
输出:
1 file 1.txt
2 file_2.txt
3 file_3.txt
...
我想使用
读取文件query=input("Please add your selection: ") # just the number
if int(query) in enumerate(datasets):
dataset=pd.read_csv('path_name'+1) # I would need to add the path to the file and, by the number, the filename
df = pd.DataFrame(dataset)
它给了我错误:TypeError: 'module' object is not iterable.
我该怎么做?
【问题讨论】:
-
您使用的答案的开头确实是个糟糕的建议。不要那样使用
index。使用enumerate查看答案的结尾或其他答案。