0

I have got number of queries from the list, and I am executing them and saving them on excel file. Now each result will be store on each sheet of the excel file. I am trying to run the queries in SQL Server, I was running the same code on Oracle and it was working, but its not working on SQL Server.

getting_list = self.create_list(connection_number)
query_sheet = os.path.abspath(os.getcwd()+'/Docs/query_results.xlsx')
# writer = pd.ExcelWriter(query_sheet)
for q in self.queries:
    df = pd.read_sql_query(q, self.conn)
    writer = pd.ExcelWriter(query_sheet, engine='xlsxwriter')
    df.to_excel(writer, sheet_name=sheet_name=str(self.queries.index(q) + 1))
    writer.save()

On running I am having this exception

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 1: invalid start byte

and this exception is appearing on following line.

df.to_excel(writer, sheet_name=sheet_name=str(self.queries.index(q) + 1))

Please Tell me where I am making mistake.

Taimoor Pasha
  • 192
  • 2
  • 3
  • 16
  • try to add encoding in the argument of your function : I would suggest encoding='iso8859-1' for example.. What language is used in your data? – Matina G Nov 20 '18 at 10:46
  • Sorry, I didn't get that.How can I achieve that? – Taimoor Pasha Nov 20 '18 at 10:50
  • `xlsx` is a zip package containing XML files. It doesn't have encodings. Supposing `query_results.xlsx` is a real `xlsx` file and not eg a `csv` or `html` file with a fake extension, what does the dataframe contain? – Panagiotis Kanavos Nov 20 '18 at 10:56
  • SQL query results. – Taimoor Pasha Nov 20 '18 at 10:58
  • @taimoorpasha obviously. What data does it contain? Text, numbers? If text, is it Unicode or encoded? Does it come from a non-Unicode database field? If so, the code will choke when it finds the first non-ANSI byte. – Panagiotis Kanavos Nov 20 '18 at 11:00
  • It contains multiple data, like phone numbers, text, intergers, even data in encrypted form as well. – Taimoor Pasha Nov 20 '18 at 11:04
  • Does this answer your question? [python: UnicodeDecodeError: 'utf8' codec can't decode byte 0xc0 in position 0: invalid start byte](https://stackoverflow.com/questions/23772144/python-unicodedecodeerror-utf8-codec-cant-decode-byte-0xc0-in-position-0-i) – skomisa Jan 08 '20 at 05:11

0 Answers0