简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM