issues with writing Chinese to csv file in Python

故事扮演 提交于 2019-12-08 11:26:38

问题


I am a python beginner here. Recently I have been trying to scrape some data from a local Chinese website. I successfully stored the information as a matrix (list of list),c, but when I was tring to write it into a csv file I have got some messy stuff. Here is the code:

from bs4 import BeautifulSoup
import requests
import pandas as pd
import csv
url = "http://wszw.hzs.mofcom.gov.cn/fecp/fem/corp/fem_cert_stat_view_list.jsp"
r=requests.get(url)
data= r.text
soup = BeautifulSoup(data) 

table = soup.find_all('table')[3]
rows = table.find_all('tr')
dogData= 0
c=[]
for tr in rows:
    cols = tr.find_all('td')
    dogName =cols[0].get_text()
    rank2013 = cols[1].get_text()
    rank2012 =cols[2].get_text()
    rank2008 =cols[3].get_text()
    rank2003 =cols[4].get_text()
    rank2004 =cols[5].get_text()
    rank2005=cols[6].get_text()
    temp=[dogName,rank2013,rank2012,rank2008,rank2003,rank2004,rank2005]
    [x.encode('gb18030') for x in temp]
    c.append(temp)



with open("output.csv", "wt") as f:
    writer = csv.writer(f)
    writer.writerows(c)

I am using Python 3.4 Can anyone tell me what went wrong and how can I improve the code? Thanks so much! Marco


回答1:


I haven't run your code, but I did notice something odd. You typed

temp=[dogName,rank2013,rank2012,rank2008,rank2003,rank2004,rank2005]
[x.encode('gb18030') for x in temp]
c.append(temp)

however [x.encode('gb18030') for x in temp] isn't doing anything. Right now, the code you wrote is the same as:

temp=[dogName,rank2013,rank2012,rank2008,rank2003,rank2004,rank2005]
c.append(temp)

If you want to use list comprehension the way you're using it:

temp=[dogName,rank2013,rank2012,rank2008,rank2003,rank2004,rank2005]
modified_temp = [x.encode('gb18030') for x in temp]
c.append(modified_temp)


来源:https://stackoverflow.com/questions/29442448/issues-with-writing-chinese-to-csv-file-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!