无法读取熊猫中的csv文件

时间:2020-09-21 14:11:55

标签: 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__.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()

我该如何解决?

1 个答案:

答案 0 :(得分:1)

阅读csv时,尝试使用encoding参数:

pd.read_csv(file_input, encoding='latin1')

pd.read_csv(file_input, encoding='iso-8859-1')

您可以参考Python Standard Encondings

的列表