python - openpyxl reads xlsx files and generates data into a dictionary. Is there a problem with Chinese encoding?
我想大声告诉你
我想大声告诉你 2017-06-12 09:25:15
0
2
1209

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.')
我想大声告诉你
我想大声告诉你

reply all(2)
大家讲道理
import codecs
codecs.open('census2010.py', 'w', 'utf-8')
淡淡烟草味

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template