在Python中写csv文件的中文问题 [英] issues with writing Chinese to csv file in Python
问题描述
我是一个python初学者。最近我一直在试图从当地的中文网站抓取一些数据。
我成功地将信息存储为一个矩阵(列表列表),c,但是当我tring写入一个csv文件我有一些杂乱的东西。这是代码:
from bs4 import BeautifulSoup
pre>
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 = []
在行中的tr:
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)
open(output.csv,wt)as f:
writer = csv.writer(f)
writer.writerows(c)
我使用Python 3.4
任何人都可以告诉我出了什么问题,我该如何改进代码?非常感谢!
Marco解决方案我没有运行你的代码,
您键入了temp = [dogName,rank2013,rank2012,rank2008,rank2003,rank2004,rank2005]
[x.encode('gb18030')for x in temp]
c.append(temp)
$ b b但是
[x.encode('gb18030')for x in temp]
没有做任何事情。现在,你写的代码是相同的:temp = [dogName,rank2013,rank2012,rank2008,rank2003,rank2004 ,rank2005]
c.append(temp)
如果要使用列表推导
temp = [dogName,rank2013,rank2012,rank2008,rank2003,rank2004,rank2005]
modified_temp = [x.encode('gb18030')for x in temp]
c.append(modified_temp)
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
解决方案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)
这篇关于在Python中写csv文件的中文问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!