search

Home  >  Q&A  >  body text

python - openpyxl reads xlsx files and generates data into a dictionary. Is there a problem with Chinese encoding?

The code below reads the data in 1.XLSX, saves all the data in the dictionary my_data, and then saves the dictionary to the file census2010.py for later use.
Question:
The operation can be successful. But in the output file, all the Chinese content is u'u5f20u5baau798f': Such encoding
How to modify the encoding so that the dictionary becomes normal Chinese characters?

import openpyxl, pprint
print('Opening workbook...')
wb = openpyxl.load_workbook('1.xlsx')
sheet=wb.get_active_sheet()
my_data={}
print('Reading rows...')
for row in range(1,125):
    cun=sheet['b'+str(row)].value
    huzhu=sheet['c'+str(row)].value
    renkou1=sheet['d'+str(row)].value
    my_data.setdefault(cun,{})
    my_data[cun].setdefault(huzhu,{'hushu':0,'renkou':0})
    my_data[cun][huzhu]['hushu']+=1
    my_data[cun][huzhu]['renkou']+=int(renkou1)
print('Writing results...')
resultFile = open('census2010.py', 'w')
resultFile.write('allData = ' + pprint.pformat(my_data))
resultFile.close()
print('Done.')
我想大声告诉你我想大声告诉你2723 days ago1210

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-06-12 09:27:15

    import codecs
    codecs.open('census2010.py', 'w', 'utf-8')

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-06-12 09:27:15

    Is it possible that it is already unicode encoded when reading?

    reply
    0
  • Cancelreply