【问题标题】:xls to xlsx in new folder-done but have error mesagexls 到 xlsx 在新文件夹中完成但有错误消息
【发布时间】:2021-01-16 11:20:23
【问题描述】:

我有大量 xls 和 xlsx 文件,我想将它们全部复制到一个新文件夹中并以 xlsx 格式... 我做到了(所有文件都在 xlsx 格式的新文件夹中),但最后我在终端出错了....为什么会这样?怎么解决?

import os


here_path= os.getcwd()

final_directory = os.path.join(here_path, r'all_xlsx')
if not os.path.exists(final_directory):
   os.makedirs(final_directory)
   

import pandas as pd

for r, d, f in os.walk(here_path):                  
    for file in f:
        if '.xls' in file:
            df = pd.read_excel(file)
            print(df)
            
            
            name_fil=os.path.splitext(file)[0]
            
            writer = pd.ExcelWriter(final_directory+"/"+name_fil+".xlsx", engine='xlsxwriter')                  
            
            df.to_excel(writer, sheet_name='Sheet1')
            
            writer.save()         

                

错误是:

[1599 rows x 29 columns]
Traceback (most recent call last):
  File "a2_all_to_xlsx_folder.py", line 38, in <module>
    df = pd.read_excel(file)
  File "/usr/local/lib/python3.8/dist-packages/pandas/util/_decorators.py", line 296, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/pandas/io/excel/_base.py", line 304, in read_excel
    io = ExcelFile(io, engine=engine)
  File "/usr/local/lib/python3.8/dist-packages/pandas/io/excel/_base.py", line 867, in __init__
    self._reader = self._engines[engine](self._io)
  File "/usr/local/lib/python3.8/dist-packages/pandas/io/excel/_xlrd.py", line 22, in __init__
    super().__init__(filepath_or_buffer)
  File "/usr/local/lib/python3.8/dist-packages/pandas/io/excel/_base.py", line 353, in __init__
    self.book = self.load_workbook(filepath_or_buffer)
  File "/usr/local/lib/python3.8/dist-packages/pandas/io/excel/_xlrd.py", line 37, in load_workbook
    return open_workbook(filepath_or_buffer)
  File "/home/kubdim/.local/lib/python3.8/site-packages/xlrd/__init__.py", line 111, in open_workbook
    with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: '1_ëÇÆ_Åä11 öôæêëåæ Çéùéåæ (çäæå 113)_éäî (ïä äïÅ.)_üÅ.xlsx'

【问题讨论】:

  • 文件名是非拉丁字母吗?
  • 文件名是希腊语,但我的 linux -kubuntu 20.04 无法识别。里面的细胞在希腊语中是可以的

标签: excel pandas save pandas.excelwriter


【解决方案1】:

试试这个:

df = pd.read_excel(file, encoding=sys.getfilesystemencoding())

或:编码='utf-8'

【讨论】:

  • 我试着接受这个错误:Traceback(最近一次调用最后一次):文件“quest.py”,第 23 行,在 df = pd.read_excel(file, encoding=sys.getfilesystemencoding ()) 文件“/usr/local/lib/python3.8/dist-packages/pandas/util/_decorators.py”,第 296 行,在 wrapper return func(*args, **kwargs) TypeError: read_excel() got意外的关键字参数“编码”
  • 尝试然后 encoding = 'cp737'
  • 在我的终端中,文件中的所有希腊字符都可以,我可以看到。只有文件名无法读取……但这对我来说没问题。我想要一个文件夹中的所有填充,并使用 xlsx 格式
  • 我说:df = pd.read_excel(file, encoding = 'cp737' ) 对吗?
  • 不工作.. Traceback(最近一次调用最后一次):文件“quest.py”,第 24 行,在 df = pd.read_excel(file, encoding = 'cp737') 文件“ /usr/local/lib/python3.8/dist-packages/pandas/util/_decorators.py",第 296 行,在 wrapper return func(*args, **kwargs) TypeError: read_excel() got an unexpected keyword argument '编码'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
  • 2017-03-28
  • 2019-03-15
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 2022-12-03
相关资源
最近更新 更多