使用循环从单独的链接连接单独文件中的子集文件



我搜索了一些可能与我的解决方案有关的其他问题, 但没有运气。 简而言之,我在同一个工作目录中有 29 个单独的文件, 我想将它们连接起来。 但是我遇到了如下错误:

File b'C:\....\filename_1206_29.csv' does not exist: 

我知道这是因为熊猫read_csv(r"link"(在那里需要一个"r"。 我尝试添加pd.read_csv(","r"(,但不起作用。

我的代码如下:

link=r"C:....{}" # I marked part of the link
for i in range(0,29):
i+=1
if len(str(int(i)))==1:
i='0'+str(int(i)) # if i is one digit add a 0 in front
else:
i=str(int(i))
filename="Scopus_FrOid_1206_{}.csv" # 29 files from 01.csv to 29.csv
search=filename.format(i)
searchstring=link.format(search) # all the links of the files
pre=r"{}"
pre.format(searchstring)
pd.read_csv(pre.format(searchstring)) # here I encountered the file not found

由于配额限制,我通常必须保存大量子集文件并将它们连接起来。 任何建议都将非常有帮助和感激! 谢谢!

感谢您的所有评论,我也提到了其他问题的解决方案,如下所示 https://stackoverflow.com/a/39534745/9340081

我用来解决我的案例的代码如下:

content=[]
for i in range(0,28):
i+=1
if len(str(int(i)))==1:
i='0'+str(int(i))
else:
i=str(int(i))
filename="""FileName_1206_{}.csv"""
search=filename.format(i)
content.append(pd.read_csv(search))
df=pd.concat(content, ignore_index=True)
df 

最新更新