Reading a UTF8 CSV file with Python

后端 未结 9 1483
青春惊慌失措
青春惊慌失措 2020-11-22 12:20

I am trying to read a CSV file with accented characters with Python (only French and/or Spanish characters). Based on the Python 2.5 documentation for the csvreader (http://

9条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 12:47

    Also checkout the answer in this post: https://stackoverflow.com/a/9347871/1338557

    It suggests use of library called ucsv.py. Short and simple replacement for CSV written to address the encoding problem(utf-8) for Python 2.7. Also provides support for csv.DictReader

    Edit: Adding sample code that I used:

    import ucsv as csv
    
    #Read CSV file containing the right tags to produce
    fileObj = open('awol_title_strings.csv', 'rb')
    dictReader = csv.DictReader(fileObj, fieldnames = ['titles', 'tags'], delimiter = ',', quotechar = '"')
    #Build a dictionary from the CSV file-> {:}
    titleStringsDict = dict()
    for row in dictReader:
        titleStringsDict.update({unicode(row['titles']):unicode(row['tags'])})
    

提交回复
热议问题