为什么我的 for 循环在 Python 中无法正常工作?
Python 中的新功能,希望你能帮助
我有一个带有这个 for 循环的脚本,用于检查目录中的文件列表,我需要做的是,如果每个文件以“xlsx”结尾,它会执行一段代码,如果它以“csv”结尾,它执行其他块代码。 我必须创建变量 extension 来提取文件的扩展名,因为如果我
for file in file_list:
#to extract the file extension:
extension = os.path.splitext(file)[1][1:]
print(file)
if extension.endswith('xlsx'):
all_df_list.append(pd.read_excel(file, header=0, engine='openpyxl'))
a = round(time.time() - start_time,2)
print(a)
elif extension.endswith('csv'):
all_df_list.append(pd.read_csv(file, chunksize=10000))
a = round(time.time() - start_time,2)
print(a)
else:
print("Not working")
运行时 使用 file ,它会显示错误 WindowsPath' object has no attribute 'endswith'在整个脚本中,它启动循环并执行第一个文件的命令,但随后一直执行到 else 语句。它应该对每个文件执行相同的操作...这些是结果(放置 xxx 来隐藏敏感信息)
Z:\DataFiles\XXX\XXX\XXX\XXX\XXX\XXX_XXX_0000.xlsx
2.43
Z:\DataFiles\XXX\XXX\XXX\XXX\XXX\XXX_XXX_2019.XLSX
Not working
Z:\DataFiles\XXX\XXX\XXX\XXX\XXX\XXX_XXX_2020.XLSX
Not working
Z:\DataFiles\XXX\XXX\XXX\XXX\XXX\XXX_XXX_2021.XLSX
Not working
Z:\DataFiles\XXX\XXX\XXX\XXX\XXX\XXXX_2022.XLSX
Not working
你能指出我做错了什么吗?
New in Python, hope ya'll can help
I have a script with this for loop that checks a list of files from a directory and what I need to do is if each file ends with 'xlsx' it does a block of code, if it ends with 'csv' it does other block code.
I had to create the variable extension to extract the extension of the file because it showed an error WindowsPath' object has no attribute 'endswith' if I used the file
for file in file_list:
#to extract the file extension:
extension = os.path.splitext(file)[1][1:]
print(file)
if extension.endswith('xlsx'):
all_df_list.append(pd.read_excel(file, header=0, engine='openpyxl'))
a = round(time.time() - start_time,2)
print(a)
elif extension.endswith('csv'):
all_df_list.append(pd.read_csv(file, chunksize=10000))
a = round(time.time() - start_time,2)
print(a)
else:
print("Not working")
When I run the whole script, it starts the loop and executes the command for the first file, but then it goes all the way down to else statement. It was supposed to do the same for each file... These are the results (put xxx to hide sensitive info)
Z:\DataFiles\XXX\XXX\XXX\XXX\XXX\XXX_XXX_0000.xlsx
2.43
Z:\DataFiles\XXX\XXX\XXX\XXX\XXX\XXX_XXX_2019.XLSX
Not working
Z:\DataFiles\XXX\XXX\XXX\XXX\XXX\XXX_XXX_2020.XLSX
Not working
Z:\DataFiles\XXX\XXX\XXX\XXX\XXX\XXX_XXX_2021.XLSX
Not working
Z:\DataFiles\XXX\XXX\XXX\XXX\XXX\XXXX_2022.XLSX
Not working
Can you point out what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于
file
是一个WindowsPath
对象(如错误所示),您可能需要使用.suffix
,并使用.lower()
将其小写与小写扩展名比较:Since
file
is aWindowsPath
object (as indicated by the error), you probably want to use.suffix
, and use.lower()
to lowercase it before comparing against lowercase extensions:您的脚本正在查找 .xlsx 和 .csv,因此 .XLSX 在 else 中进行处理:
块。
当您将值分配给变量扩展名时,可以使用函数
.lower()
。Your script is looking for .xlsx and .csv and therefore .XLSX is treated in the else:
block.
You could use the function
.lower()
when you assign the value to the variable extension.我尝试了不同的解决方案,按照建议,我只是使用了:
并且它有效。多谢!!!
I tried different solutions and as suggested, I just used:
and it worked. Thanks a lot!!!
不能在循环外定义
extension = os.path.splitext(file)[1][1:]
吗?Can't you define
extension = os.path.splitext(file)[1][1:]
outside the loop?使用
pathlib
for Pathing 您可以得到字符串来自.name
!Use
pathlib
for pathing and you can get the string from.name
!