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.')
Is it possible that it is already unicode encoded when reading?