Python multiple special characters in csv file

Viewed 85

I have an csv file with some data that contains string like this:

string = 'übung Überprüfung'

and I want my programm to read it or convert it into the right format:

'übung Überprüfung'

My current solution:

print(string.encode('cp1252', 'ignore').decode('utf-8','replace'))

Output: �bung Überprüfung

I have been struggling with this for ours and days. Maybe someone can help me.

1 Answers

Assuming the whole file is encoded in cp1252 (we are not dealing with an incorrectly encoded file), the best approach would be to specify it's encoding when the file is opened:

with open("data.csv", "r", encoding="cp1252") as csvfile:
    ...
Related