我有几个csv文件,它们被命名为:list_csv_X
,其中X从0到5。我想用Python做一个循环来读取它们。
我现在正在手动操作:
for filepath in list_csv_0:
with open(filepath,'r',encoding="utf8", errors='ignore') as csvfile:
BLABLABLA
for filepath in list_csv_1:
with open(filepath,'r',encoding="utf8", errors='ignore') as csvfile:
BLABLABLA
for filepath in list_csv_2:
with open(filepath,'r',encoding="utf8", errors='ignore') as csvfile:
BLABLABLA
我该怎么做呢?
多谢了!
2条答案
按热度按时间oxalkeyp1#
使用一个简单的 for 循环和一个f-string,如下所示:
5ssjco0h2#
如果我没理解错的话,你的6个项目都是一个 * 文件名列表 *,对吧?
我假设,因为你正在这样做,并说它目前有效:
如果
list_csv_0
是一个文件名,这将导致错误,因为filepath
值将是文件名的单个字符,而不是整个文件名。可以循环列表