Python 3.x 无法读取熊猫中的csv文件
我正在编写一个程序,允许用户选择一个excel文件,并以某种方式将其重新格式化为.txt文件。当使用文件扩展名.xlsx时,它能够按预期工作。但如果文件为.csv格式,则会读出以下错误:Python 3.x 无法读取熊猫中的csv文件,python-3.x,pandas,csv,Python 3.x,Pandas,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__.
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()
如何解决此问题?读取csv时,请尝试使用
编码
参数:
pd.read_csv(file_input, encoding='latin1')
或
如果我使用此代码,您可以参考列表:
df=pd.read\u csv(文件输入,编码为utf\u 8')
,它将发出以下错误:UnicodeDecodeError:“utf-8”编解码器无法解码位置0处的字节0xff:无效的开始字节
,这就是我尝试其他编码的原因。您建议的编码将输出一些中文字符。你有没有其他你认为有效的方法?嗯,很难说。我提出了一个编码问题。这里有一个可以尝试的值列表:
pd.read_csv(file_input, encoding='iso-8859-1')