Python中文乱码问题总结汇总

读写文件

with open('maoyan_result.txt', 'a', encoding='utf-8') as f:
        f.write(json.dumps(content, ensure_ascii=False) + '\n')
        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()

获取网页

html=requests.get(url)
html.encoding = 'GBK'
#万能法
response.encoding=response.apparent_encoding

你可能感兴趣的:(Python,Python,爬虫,matplotlib,乱码)