Marco Dinatsoli
Marco Dinatsoli

Reputation: 10570

unicode issue in python when writing to file

I have an csv sheet that i read it like this:

  with open(csvFilePath, 'rU') as csvFile:
        reader = csv.reader(csvFile, delimiter= '|')
        numberOfMovies = 0
        for row in reader:
            title = row[1:2][0]

as you see, i am taking the value of title

Then i surf the internet for some info about that value and then i write to a file, the writing is like this:

def writeRDFToFile(rdf, fileName):
    f = open("movies/" + fileName + '.ttl','a')
    try:
        #rdf = rdf.encode('UTF-8')
        f.write(rdf) # python will convert \n to os.linesep
    except:
        print "exception happened for movie " + movieTitle
    f.close()

In that function, i am writing the rdf variable to a file.

As you see there is a commetted line

If the value of rdf variable contains unicode char and that line was not commeted, that code doesn't write anything to the file.

However, if I just commet that line, that code writes to a file.

Okay you can say that: commit that line and everything will be fine, but that is not correct, because i have another java process (which is Fuseki server) that reads the file and if the file contains unicode chars, it throws an error.

so i need to solve the file myself, i need to encode that data to ut8,

help please

Upvotes: 0

Views: 86

Answers (1)

Newb
Newb

Reputation: 2930

The normal csv library can have difficulty writing unicode to files. I suggest you use the unicodecsv library instead of the csv library. It supports writing unicode to CSVs.

Practically speaking, just write:

import unicodecsv as csv

Upvotes: 2

Related Questions