search

Home  >  Q&A  >  body text

网页爬虫 - Python:爬虫的中文编码问题?

爬取中文网页后正则匹配出中文,得打UTF-8的编码字符

将其输出为.csv文件

在.CSV中显示为乱码

用记事本打开.csv又可以正常显示为中文

有没有大神指点是怎么一回事?怎样才能在Excel里直接看到中文?

ringa_leeringa_lee2785 days ago566

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:28:54

    The simple way is to use pandasto_excel方法转化成.xlsx文件,因为.xlsxThe default encoding supports Excel by default. The difference is of course that it cannot be opened with Notepad.

    import pandas as pd
    
    a = pd.read_csv('./test.csv')
    
    a.to_excel('./test_output.xlsx', index=False)
    
    a.to_excel('./test_output.csv', index=False)

    I don’t have windows here to test, you can try to write the encoding as gb2312或者gbk.

    For table file I/O, pandas is actually more convenient.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:28:54

    abs1=abs1.decode().encode('gbk')

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:28:54

    Excel uses GBK encoding by default.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:28:54

    Create a new excel file, then click Data from text to import the csv file

    reply
    0
  • Cancelreply