从Urlib写入CSV并正确管理编码

2024-05-29 08:17:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要将向量/列表的内容放入CSV。 显然,我在“python编码问题”上遇到了麻烦。在

下面是我们要讲的代码:

import pdb
#pdb.set_trace()
import sys
sys.version_info
import csv
from bs4 import *
import urllib.request
rows=list()


def parse_csv(content, delimiter = ';'):
  csv_data = []
  for line in content.split('\n'):
    csv_data.append( [x.strip() for x in line.split( delimiter )] ) # strips spaces also
  return csv_data



list_url=parse_csv(open('url.csv','rU').read())

for i in range (0,len(list_url)):
    url=str(list_url[i][0]) ## read URL from an array coming from an Url-CSV
    page=urllib.request.urlopen(url)
    soup = BeautifulSoup(page.read(),"html.parser")

    for h1 in soup.find_all('h1'):
        rows.append(h1.get_text().encode('utf-8').strip()) #looks for title

    for h2 in soup.find_all('h2'):
        rows.append(h2.get_text().encode('utf-8').strip()) #looks for title

    restricted_webpage= soup.find( "div", {"id":"ingredients"} )
    readable_restricted=str(restricted_webpage)

    soup2=BeautifulSoup(readable_restricted,"html.parser")

    for td in soup2.find_all('td'):
        rows.append(td.get_text().encode('utf-8').strip())

print(rows)

if sys.version_info >= (3,0,0):
    f = open('FB.csv', 'w', newline='')
else:
    f = open('FB.csv', 'wb')


wr = csv.writer(f)
wr.writerows(rows)

如果我这么做的话。我的CSV看起来是这样的:(这一点都不好)

^{pr2}$

这意味着我必须解码CSV才能在将来使用它。这对我没用。我需要一些“视觉”的东西。在

如果我试着替换

   encode('utf-8').strip()

签署人:

strip()

我收到错误:

wr.writerows(rows) UnicodeEncodeError: 'ascii' codec can't encode character '\xe8' in position 8: ordinal not in range(128)
strip

在这里非常有用,因为它可以帮助我摆脱我不喜欢的'\n'

我尝试过的第二件事是:通过向ASCI求助来摆脱那些和其他麻烦制造者:

yourstring = yourstring.encode('ascii', 'ignore').decode('ascii')

结果还不错,但还不够:

"
",A,d,u,l,t, ,S,m,a,l,l, ,D,o,g
D,e, ,p,l,u,s, ,d,e, ,8,/, ,1,0, ,m,o,i,s,.
"
", , , , , , , , , , , , , , , , ,C,e,n,d,r,e,s, ,b,r,u,t,e,s, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,7,.,4,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,e,l,l,u,l,o,s,e, ,b,r,u,t,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,.,6,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,F,i,b,r,e,s, ,a,l,i,m,e,n,t,a,i,r,e,s, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,6,.,6,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,M,a,t,i,r,e, ,g,r,a,s,s,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,6,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,A,c,i,d,e, ,l,i,n,o,l,i,q,u,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,.,1,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,E,n,e,r,g,i,e, ,m,t,a,b,o,l,i,s,a,b,l,e, ,(,c,a,l,c,u,l,e, ,s,e,l,o,n, ,N,R,C,8,5,), ,(,k,c,a,l,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,6,5,2,.,5,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,E,n,e,r,g,i,e, ,m,t,a,b,o,l,i,s,a,b,l,e, ,(,m,e,s,u,r,e,), ,(,k,c,a,l,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,9,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,H,u,m,i,d,i,t, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,9,.,5,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,E,x,t,r,a,i,t, ,n,o,n, ,a,z,o,t, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,4,0,.,5,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,O,m,g,a, ,6, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,.,1,8,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,P,r,o,t,i,n,e, ,b,r,u,t,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,5,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,A,m,i,d,o,n, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,5,.,5,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,h,l,o,r,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,.,4,3,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,u,i,v,r,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,5,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,I,o,d,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,.,9,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,F,e,r, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,6,7,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,M,a,n,g,a,n,s,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,6,8,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,Z,i,n,c, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,4,2,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,B,i,o,t,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,.,1,3,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,h,o,l,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,6,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,A,c,i,d,e, ,f,o,l,i,q,u,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,3,.,9,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,A, ,(,U,I,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,2,0,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,B,1, ,T,h,i,a,m,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,7,.,5,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,B,2, ,R,i,b,o,f,l,a,v,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,4,9,.,6,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,B,3, ,N,i,a,c,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,4,9,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,B,5, ,A,c,i,d,e, ,p,a,n,t,o,t,h,n,i,q,u,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,4,7,.,8,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,B,6, ,P,y,r,i,d,o,x,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,7,7,.,1,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,C, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,D,3, ,(,U,I,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,8,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,E, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,6,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,A,r,g,i,n,i,n,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,.,5,3,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,L,u,t,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,5,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,M,t,h,i,o,n,i,n,e, ,C,y,s,t,i,n,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,.,1,8,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,T,a,u,r,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,9,0,0,.,0,"
", , , , , , , , 
"
",B,e,a,g,l,e, ,A,d,u,l,t
B,e,a,g,l,e, ,a,d,u,l,t,e,",", , ,p,a,r,t,i,r, ,d,e, ,1,2, ,m,o,i,s
"
", , , , , , , , , , , , , , , , ,A,c,i,d,e, ,a,r,a,c,h,i,d,o,n,i,q,u,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,0,7,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,e,n,d,r,e,s, ,b,r,u,t,e,s, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,6,.,1,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,B,i,o,t,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,.,9,2,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,F,i,b,r,e,s, ,a,l,i,m,e,n,t,a,i,r,e,s, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,A,c,i,d,e, ,l,i,n,o,l,i,q,u,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,.,4,2,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,L,u,t,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,5,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,E,n,e,r,g,i,e, ,m,t,a,b,o,l,i,s,a,b,l,e, ,(,c,a,l,c,u,l,e, ,s,e,l,o,n, ,N,R,C,8,5,), ,(,k,c,a,l,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,4,2,5,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,E,n,e,r,g,i,e, ,m,t,a,b,o,l,i,s,a,b,l,e, ,(,m,e,s,u,r,e,), ,(,k,c,a,l,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,5,8,4,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,M,t,h,i,o,n,i,n,e, ,C,y,s,t,i,n,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,9,2,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,H,u,m,i,d,i,t, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,9,.,5,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,E,x,t,r,a,i,t, ,n,o,n, ,a,z,o,t, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,4,1,.,7,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,O,m,g,a, ,3, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,6,4,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,O,m,g,a, ,6, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,.,6,6,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,P,h,o,s,p,h,o,r,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,7,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,P,r,o,t,i,n,e, ,b,r,u,t,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,7,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,A,m,i,d,o,n, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,4,.,4,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,T,a,u,r,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,5,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,A, ,(,U,I,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,9,0,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,C, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,E, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,6,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,a,l,c,i,u,m, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,9,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,P,o,l,y,p,h,n,o,l,s, ,d,e, ,t,h, ,v,e,r,t, ,e,t, ,d,e, ,r,a,i,s,i,n,s, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,5,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,h,l,o,r,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,6,3,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,u,i,v,r,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,5,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,I,o,d,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,4,.,8,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,F,e,r, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,1,2,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,M,a,g,n,s,i,u,m, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,0,8,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,M,a,n,g,a,n,s,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,7,1,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,P,o,t,a,s,s,i,u,m, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,7,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,S,l,n,i,u,m, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,2,9,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,S,o,d,i,u,m, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,4,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,Z,i,n,c, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,0,1,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,h,o,l,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,2,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,A,c,i,d,e, ,f,o,l,i,q,u,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,2,.,9,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,B,1, ,T,h,i,a,m,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,5,.,7,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,B,1,2, ,C,y,a,n,o,c,o,b,a,l,a,m,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,1,3,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,B,2, ,R,i,b,o,f,l,a,v,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,4,6,.,2,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,B,3, ,N,i,a,c,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,4,5,8,.,7,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,B,5, ,A,c,i,d,e, ,p,a,n,t,o,t,h,n,i,q,u,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,3,7,.,9,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,B,6, ,P,y,r,i,d,o,x,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,7,2,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,V,i,t,a,m,i,n,e, ,D,3, ,(,U,I,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,8,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,A,r,g,i,n,i,n,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,.,5,2,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,L,-,l,y,s,i,n,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,.,0,8,"
", , , , , , , , 
"
",J,a,c,k, ,R,u,s,s,e,l,l, ,A,d,u,l,t
J,a,c,k, ,R,u,s,s,e,l,l, ,T,e,r,r,i,e,r, ,a,d,u,l,t,e,",", , ,p,a,r,t,i,r, ,d,e, ,1,0, ,m,o,i,s
"
", , , , , , , , , , , , , , , , ,A,c,i,d,e, ,a,r,a,c,h,i,d,o,n,i,q,u,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,0,7,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,e,n,d,r,e,s, ,b,r,u,t,e,s, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,5,.,9,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,B,i,o,t,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,.,0,7,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,F,i,b,r,e,s, ,a,l,i,m,e,n,t,a,i,r,e,s, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,7,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,D,L,-,m,t,h,i,o,n,i,n,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,6,5,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,E,P,A,/,D,H,A, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,0,.,3,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,M,a,t,i,r,e, ,g,r,a,s,s,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,6,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,e,l,l,u,l,o,s,e, ,b,r,u,t,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,.,3,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,C,h,l,o,r,u,r,e, ,d,e, ,g,l,u,c,o,s,a,m,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,4,9,5,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,G,l,u,c,o,s,a,m,i,n,e, ,p,l,u,s, ,c,h,o,n,d,r,o,t,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,5,0,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,L,-,c,a,r,n,i,t,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,1,5,0,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,A,c,i,d,e, ,l,i,n,o,l,i,q,u,e, ,(,%,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,2,.,8,8,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,L,u,t,i,n,e, ,(,m,g,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,5,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,E,n,e,r,g,i,e, ,m,t,a,b,o,l,i,s,a,b,l,e, ,(,c,a,l,c,u,l,e, ,s,e,l,o,n, ,N,R,C,8,5,), ,(,k,c,a,l,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,7,1,6,.,0,"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,E,n,e,r,g,i,e, ,m,t,a,b,o,l,i,s,a,b,l,e, ,(,m,e,s,u,r,e,), ,(,k,c,a,l,/,k,g,),"
", , , , , , , , 
"
", , , , , , , , , , , , , , , , ,3,9,6,4,.,0,"
", , , , , , , , 

当我试图另外添加一个strip时。生成的CSV为空。在

现在只要一个strip就会产生一个错误:

UnicodeEncodeError: 'ascii' codec can't encode character '\xe8' in position 8: ordinal not in range(128)

我读过并尝试过这篇文章的大部分内容{a1}

ASCII的方式不太好,因为我丢失了信息。我确实需要它们。在

我怎样才能把我的单子放在这里面越野车.csv? 在


Tags: csvinimporturlforfindlistutf
2条回答

看起来您使用的是python3。BeautifulSoup返回Unicode字符串,csv接受Unicode字符串,因此不要对它们进行编码。在

writerows获取列文本列表。看起来您只传递了一个文本列表,因此字符串被枚举并一次给出一个字符。在

以下是将编码字符串列表提供给csv.writerows

>>> import csv
>>> with open('test.csv','w',newline='') as f:
...  w=csv.writer(f)
...  rows = ['abc,def'.encode('utf8'),'ghi,jkl'.encode('utf8')]
...  w.writerows(rows)
...
>>>

C:\>type test.csv
97,98,99,44,100,101,102
103,104,105,44,106,107,108

下面是当您给出一个Unicode字符串列表而不是一个Unicode字符串列表时发生的情况。注意:要指定Unicode字符串的编码,请在open中指定:

^{pr2}$

注:代码页65001为UTF-8。正确切换字符:

C:\>chcp 65001
Active code page: 65001

C:\>type test.csv
è,a,b,c,",",d,e,f
è,d,e,f,",",g,h,i

以下是如何获得正确的输出:

>>> import csv
>>> with open('test.csv','w',encoding='utf8',newline='') as f:
...  w=csv.writer(f)
...  rows = [['abcd\xe8','d\xe8fg'],['ghijk','lmnop']]
...  w.writerows(rows)
...
>>>

C:\>type test.csv
abcdè,dèfg
ghijk,lmnop

用下面的说明测试您的代码:

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

你应该看看here

相关问题 更多 >

    热门问题