2

I am trying to convert all csv files of a folder to xlsx and using below code.

import glob
import csv
import pandas as pd
files = glob.glob('D:\cf111\*.csv')
for k in files:
    df = pd.read_csv(k)
    df.to_excel("abc.xlsx")

The code is generating the below error.

utf-8' codec can't decode byte 0x92 in position 11: invalid start byte

I cant sort out, how to resolve this error.

Husnain Iqbal
  • 89
  • 1
  • 10

2 Answers2

1

The problem might because there are contents in the csv that do not support encoding="utf-8". you can try using other encoding though.

code example:

import glob
import csv
import pandas as pd
files = glob.glob('D:\cf111\*.csv')
for k in files:
    df = pd.read_csv(k, encoding='ISO 8859-1')
    df.to_excel("abc.xlsx")

document reference: https://docs.python.org/3/library/codecs.html#standard-encodings

Lurima
  • 52
  • 3
  • Thank you. The encoding problem is resolved. but it only converts one(first) file. Does not run through the whole folder to check other files. – Husnain Iqbal Mar 11 '20 at 04:01
  • You need to change your excel file names each time the **for loop** executes the loop. Else, only your **last** csv will be converted. – Lurima Mar 11 '20 at 05:08
1

Use the ISO 8859-1 encoding instead of 'utf-8', for example

msg = email.message_from_string(str(arr[1],  'ISO 8859-1'))
A. K.
  • 34,395
  • 15
  • 52
  • 89
Ray
  • 124
  • 7