Python 输入()后将excel文件另存为数据框

Python 输入()后将excel文件另存为数据框,python,excel,pandas,Python,Excel,Pandas,我想从用户那里获取文件路径输入,然后使用pandas对文件进行操作 到目前为止,我做了以下工作: import sys import os import pandas as pd user_input = input("Enter the path of your file: ") assert os.path.exists(user_input), "I did not find the file at, "+str(user_input) f = open(user_input,'r+'

我想从用户那里获取文件路径输入,然后使用pandas对文件进行操作

到目前为止,我做了以下工作:

import sys
import os
import pandas as pd

user_input = input("Enter the path of your file: ")

assert os.path.exists(user_input), "I did not find the file at, "+str(user_input)
f = open(user_input,'r+') 
我假设我猜,我在这里出错了,文件被临时保存在f中

在这之后,我做了以下工作

xl = pd.ExcelFile(f)
这是行不通的

get的错误是

Enter the path of your file: C:/Users/MyPC/Desktop/Files/Data/11.05.2018/data.xls
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-6-9bfb9b9b2c74> in <module>()
      7 assert os.path.exists(user_input), "I did not find the file at, "+str(user_input)
      8 f = open(user_input,'r+')
----> 9 xl = pd.ExcelFile(f)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\io\excel.py in __init__(self, io, **kwds)
    289         elif not isinstance(io, xlrd.Book) and hasattr(io, "read"):
    290             # N.B. xlrd.Book has a read attribute too
--> 291             data = io.read()
    292             self.book = xlrd.open_workbook(file_contents=data)
    293         elif isinstance(self._io, compat.string_types):

~\AppData\Local\Continuum\anaconda3\lib\encodings\cp1252.py in decode(self, input, final)
     21 class IncrementalDecoder(codecs.IncrementalDecoder):
     22     def decode(self, input, final=False):
---> 23         return codecs.charmap_decode(input,self.errors,decoding_table)[0]
     24 
     25 class StreamWriter(Codec,codecs.StreamWriter):

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 545: character maps to <undefined>
有人能帮我吗


注意。

首先,您需要将输入保存为数据框,然后将其保存为excel格式。我猜输入文件的格式也是excel。我会这样做:

import sys
import os
import pandas as pd

user_input = input("Enter the path of your file: ")

assert os.path.exists(user_input), "I did not find the file at, "+str(user_input)

df = pd.read_excel(user_input)
现在可以运行df来查看数据帧


为了保存,请查看此文档:

刚刚编辑了答案,因为我注意到您已经打印了输入文件扩展名。您提到的过程将涉及再次输入文件名。但是,我已经将该文件作为f的输入。好吧,如果您将该文件作为df=pd.read\u exceluser\u input传递会怎么样?printuser\u input的输出是什么?它应该是一个带有输入文件名的字符串。我试过了,符合熊猫的要求。我正在编辑我的答案,使其更符合要求