python-3.x - 无法在熊猫中读取 csv 文件
问题描述
我正在编写一个程序,它允许用户选择一个 excel 文件并以某种方式将其重新格式化为 .txt 文件。当使用文件扩展名 .xlsx 时,它能够按预期工作。但是如果文件是 .csv,它会读出这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\davew\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/davew/Downloads/excel_to_txt2.py", line 27, in btn_convert_pressed
df = pd.read_csv(file_input)
File "C:\Users\davew\Desktop\excel_to_note\venv\lib\site-packages\pandas\io\parsers.py", line 686, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:\Users\davew\Desktop\excel_to_note\venv\lib\site-packages\pandas\io\parsers.py", line 452, in _read
parser = TextFileReader(fp_or_buf, **kwds)
File "C:\Users\davew\Desktop\excel_to_note\venv\lib\site-packages\pandas\io\parsers.py", line 936, in __init__
self._make_engine(self.engine)
File "C:\Users\davew\Desktop\excel_to_note\venv\lib\site-packages\pandas\io\parsers.py", line 1168, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:\Users\davew\Desktop\excel_to_note\venv\lib\site-packages\pandas\io\parsers.py", line 1998, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas\_libs\parsers.pyx", line 537, in pandas._libs.parsers.TextReader.__cinit__
File "pandas\_libs\parsers.pyx", line 740, in pandas._libs.parsers.TextReader._get_header
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
这是我用来转换和重新格式化文件的代码:
from tkinter import *
from tkinter import filedialog
import pandas as pd
file_input = ''
colour_btn_excel = '#8df0a8'
colour_btn_save = '#f0b789'
# Button Choose Excel pressed
def btn_input_pressed():
global file_input
file_input = filedialog.askopenfilename(title="Select a File")
if file_input != '':
txt_input.set(file_input.split('/')[-1])
# Button Save as text file pressed
def btn_convert_pressed():
if file_input != '':
file_output = filedialog.asksaveasfilename(title="Select a File")
if file_output != '':
if file_input.endswith('xlsx'):
df = pd.read_excel(file_input)
else:
df = pd.read_csv(file_input)
with open(file_output, mode='w') as f:
for name, value in df.iteritems():
f.write(str(name) + ':\n' + str(value[0]) + '\n\n')
# setup of the window
width = 400
height = 130
x_pos = 200
y_pos = 200
root = Tk()
root.geometry('{}x{}+{}+{}'.format(width, height, x_pos, y_pos))
root.title('Convert excel to text file')
txt_input = StringVar()
# Frame Choose Excel file
fr_excel = Frame(root)
fr_excel.place(relx=0.05, rely=0.05, relwidth=0.9, relheight=0.6)
btn_input = Button(fr_excel, text='Choose excel file', command=btn_input_pressed, width=15, relief='flat', bg=colour_btn_excel)
btn_input.grid(row=0, column=0, padx=20, pady=20)
lbl_input = Label(fr_excel, textvariable=txt_input)
lbl_input.grid(row=0, column=1, padx=15, pady=20)
# Button Save as text file
btn_convert = Button(root, text='Save as text file', command=btn_convert_pressed, relief='flat', bg=colour_btn_save)
btn_convert.place(relx=0.65, rely=0.7, relwidth=0.3, relheight=0.2)
root.mainloop()
我该如何解决?
解决方案
encoding
读取 csv 时尝试使用参数:
pd.read_csv(file_input, encoding='latin1')
或者
pd.read_csv(file_input, encoding='iso-8859-1')
您可以参考Python 标准编码列表
推荐阅读
- git - 如何使用来自 dev 分支的一行命令更新本地 master 和 rebase 到它?
- html - 将 json 文件作为参数传递是否会使渲染 html 模板失去性能?
- azure-api-management - 用户名/密码登录和注册仍然可用,但应禁用
- javascript - 更新存储中的数据后如何重新渲染组件
- flutter - 扩展从另一个文件调用的 Widget
- c++ - 系统(“暂停”)或保持窗口打开()?
- reactjs - 动态图像未渲染
- python - 查找 24 小时范围内的最大值,并提取该行的所有属性
- powershell - 需要附加这个powershell脚本,不知道怎么办
- javascript - 拖动不适用于反应使用手势