Python“带有开放式” CSV vs XLS vs XLSX
有人知道如果文件是CSV或XLS而不是XLSX,为什么以下代码有效?
with open("file", 'w') as ft:
for row in auto_rep[answer][:-1]:
if row != "":
ft.write(row.replace(";",",")+"\n")
ft.write(auto_rep[answer][-1].replace(";",","))
ft.close()
我希望该文件是XLSX。我不确定是否得到支持。
例如:文件是
- fileName.csv
- filename.xls
- filename.xlsx
我也首先使用以下代码来复制xlsx文件,这样我就不会对原始文件感到烦恼。
import shutil
orig = r'C:\filepath\original.xlsx‘
targ = r'C:\filepath\file(.csv, .xls, or .xlsx)'
shutil.copyfile(orig, targ)
如果文件为CSV,则创建文件,并且填充到第一张纸的信息是正确的。我知道CSV不使用多纸,所以我知道无论如何它不会加载其他人。
如果文件为XLS,它将创建文件,但我仍然会收到以下消息:
“文件格式和'filename.xls'的扩展不匹配。该文件可能会损坏或不安全。除非您相信它的来源,否则请不要打开它。你想打开它吗?” …我可以单击“是”,但只有第一张纸填充。
如果文件为XLSX,它将创建文件,但我会发现一个错误,该错误指出:
“ Excel无法打开文件'filename.xlsx',因为文件格式或文件扩展名无效。验证文件尚未损坏,并且文件扩展名与文件的格式匹配。”
Does anyone know why the below code works if the file is csv or xls but not for xlsx?
with open("file", 'w') as ft:
for row in auto_rep[answer][:-1]:
if row != "":
ft.write(row.replace(";",",")+"\n")
ft.write(auto_rep[answer][-1].replace(";",","))
ft.close()
I would like the file to be xlsx. I’m not sure if this is supported though.
Ex: file is either
- filename.csv
- filename.xls
- filename.xlsx
I am also using the following code first to copy an xlsx file so I don’t mess with the original file.
import shutil
orig = r'C:\filepath\original.xlsx‘
targ = r'C:\filepath\file(.csv, .xls, or .xlsx)'
shutil.copyfile(orig, targ)
If the file is csv, it creates the file and the information populated to the first sheet is correct. I know csv doesn’t use multi sheets so I understand it wouldn’t load the others anyways.
If the file is xls, it creates the file but I still receive the following message:
“The file format and extension of ‘filename.xls’ don’t match. The file could be corrupted or unsafe. Unless you trust it’s source, don’t open it. Do you want to open it anyway?” … and I can click yes and it loads but only the first sheet populates.
If the file is xlsx, It creates the file but I get an error that states:
“Excel cannot open the file ‘filename.xlsx’ because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.”
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在研究一个simliar问题,我发现这个问题的答案可能会有所帮助。 如何检查上传的文件是CSV或python中的XLS?
由于这个问题只有几个月大,所以我希望这仍然有所帮助。它使用标准库软件包,因此对您来说应该可以。
我将此答案应用于您的特定问题的方式是使用PANDAS阅读任何一个Filetype,然后将其保存到Excel格式。不过,您需要进口大熊猫。诚然,您是“ ...弄乱原始文件……”,但也许您可以独自一人离开XLS,只为CSV做到这一点。
I'm researching a simliar question and I found this question with an answer that might help. How to check the uploaded file is csv or xls in python?
Since this question is only a few months old I am hoping this will still help. It uses a standard library package so that should be ok for you.
The way I would apply this answer to your specific question would be to read either filetype using PanDAS and then save it to Excel format. You would need to import PanDAS though. Admittedly you are "...messing with the original file..." but perhaps you could leave the xls's alone and only do this for csv.