Сохранить файл Excel в качестве фрейма данных после ввода () - PullRequest
0 голосов
/ 23 мая 2018

Я хочу получить путь к файлу от пользователя, а затем выполнить манипуляции с файлом, используя 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>

Может ли кто-нибудь мне помочь?

С уважением.

1 Ответ

0 голосов
/ 23 мая 2018

Сначала вам нужно сохранить свои данные как pandas DataFrame, а затем сохранить их в формате 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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...