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

真理报

我想从用户那里获取文件路径输入,然后使用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)

这不起作用。

得到的错误是,

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>

有人能帮我吗?

问候。

el_里纳尔多

首先,您需要将输入保存为 pandas DataFrame,然后将其保存为 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以查看数据框。

如需保存,请查看此文档:https : //pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Excel另存为框

将excel另存为pdf,将文件名另存为变量值

使用新输入将单个excel工作表另存为新的excel文件

迭代和拆分 Excel 文件名并在 Pandas 中另存为数据框

将Python数据框另存为Teradata中的表

将HTML图像输入另存为Java文件

VBA Excel将副本另存为txt文件

将Excel工作表另存为JSON文件

VBA从Access将Excel文件另存为.xls

使用for循环将数据帧另存为.Rdata文件

python matplotlib将图形另存为数据文件

将课程数据另存为文本文件

将 PDF 文件另存为“数据” - 使用 PHP MPDF

使用熊猫将数日长的数据框拆分为半小时的数据框,并将其另存为csv文件

Rails将文件另存为图像

将pdf文件另存为blob

将图另存为EMF文件

将.maf文件另存为表

C#(Microsoft.Office.Interop.Excel),将数据另存为Excel 2003文件(xls)

如何使用AppleScript在Firefox的“另存为”对话框中输入文件名?

VBA:Word将.docx文件另存为.doc文件,如何另存为docx?

将带有数字列的Pandas数据框另存为Excel中的文本

将带有列表列的数据框另存为csv文件

如何从网站下载xlsx文件以使用Pandas另存为数据框

拆分数据框,重新排列并另存为单独的 csv 文件

将熊猫另存为Excel文件时,如何使用熊猫插入Excel公式?

将列表另存为 Pandas 数据框列中的列表

如何将另存为RData的R中的数据框导入熊猫?

将Spark数据框另存为Hive中的动态分区表