Python中国語文字化けし問題まとめ


ファイルの読み書き
with open('maoyan_result.txt', 'a', encoding='utf-8') as f:
        f.write(json.dumps(content, ensure_ascii=False) + '
') f.close()
f= open('news6', 'w', encoding='utf-8-sig', newline='')
writer = csv.writer(f)
head=['time','title','content','url','key']
writer.writerow(head)
with open(output_csv_file, 'w', newline='', encoding='gb18030') as fa:
      writer = csv.writer(fa)

絵をかく
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
l1,=plt.plot([2,3,4,5],[6,7,3,5],'red',label='line1')
l2,=plt.plot([2,3,4,5],[2,4,8,3],'green',label='line2')
plt.legend(handles=[l1,l2])
plt.show()

Webページの取得
html=requests.get(url)
html.encoding = 'GBK'
#   
response.encoding=response.apparent_encoding