Hi everyone,
I’m trying to code an experiment for German speakers, where stimuli are read in from a .csv file, which contains special characters like äöü and ß. However, despite reading extensively about inputting this type of text, I simply can’t figure out how to do it.
My input file is this one:
data.csv (82 Bytes)
And it looks something like this:
1 Auffahrunfall 1
2 Überholen 1
3 Balkon 1
4 Traktor 1
My current attempt at reading in this file is the following:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import codecs
f = codecs.open('data.csv', encoding='utf-8')
for line in f:
print str(line)
However, the program gets only as far as outputting
1,Auffahrunfall,1
and then gives me the error message:
UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xdc’ in position 2: ordinal not in range(128)
I don’t understand why an ascii codec is at work here, after all I’m using a utf-8 encoded input file, and I’m asking PsychoPy to use utf-8 encoding when reading the file.
Moreover, I can’t seem to find a solution to the problem. I thought perhaps adding
errors='ignore'
to the codecs.open() command would help, but it doesn’t.
I would be grateful if you could push me in the right direction on this one…
Thanks you!