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

时间:2015-07-20 22:50:22

标签: python csv encoding utf-8 beautifulsoup

我需要将矢量/列表的内容放入CSV中。 我明显遇到了“python编码问题”的麻烦。

以下是我们关于的代码:

Exception calling "DownloadString" with "1" argument(s): "An exception occurred during a WebClient request."
At line:1 char:17
+ &{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.gith ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

如果我这样做。我的CSV看起来像这样:(这根本不好)

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以便将来使用它。这对我不起作用。我需要“视觉”的东西。

如果我尝试更换每个

65,100,117,108,116,32,83,109,97,108,108,32,68,111,103
68,101,32,112,108,117,115,32,100,101,32,56,47,32,49,48,32,109,111,105,115,46
67,101,110,100,114,101,115,32,98,114,117,116,101,115,32,40,37,41
55,46,52
67,101,108,108,117,108,111,115,101,32,98,114,117,116,101,32,40,37,41
49,46,54
70,105,98,114,101,115,32,97,108,105,109,101,110,116,97,105,114,101,115,32,40,37,41
54,46,54
77,97,116,105,195,168,114,101,32,103,114,97,115,115,101,32,40,37,41
49,54,46,48
65,99,105,100,101,32,108,105,110,111,108,195,169,105,113,117,101,32,40,37,41
51,46,49
69,110,101,114,103,105,101,32,109,195,169,116,97,98,111,108,105,115,97,98,108,101,32,40,99,97,108,99,117,108,195,169,101,32,115,101,108,111,110,32,78,82,67,56,53,41,32,40,107,99,97,108,47,107,103,41
51,54,53,50,46,53
69,110,101,114,103,105,101,32,109,195,169,116,97,98,111,108,105,115,97,98,108,101,32,40,109,101,115,117,114,195,169,101,41,32,40,107,99,97,108,47,107,103,41
51,57,48,48,46,48
72,117,109,105,100,105,116,195,169,32,40,37,41
57,46,53
69,120,116,114,97,105,116,32,110,111,110,32,97,122,111,116,195,169,32,40,37,41
52,48,46,53
79,109,195,169,103,97,32,54,32,40,37,41
51,46,49,56
80,114,111,116,195,169,105,110,101,32,98,114,117,116,101,32,40,37,41
50,53,46,48
65,109,105,100,111,110,32,40,37,41
51,53,46,53
67,104,108,111,114,101,32,40,37,41
49,46,52,51
67,117,105,118,114,101,32,40,109,103,47,107,103,41
49,53,46,48
73,111,100,101,32,40,109,103,47,107,103,41
50,46,57
70,101,114,32,40,109,103,47,107,103,41
49,54,55,46,48
77,97,110,103,97,110,195,168,115,101,32,40,109,103,47,107,103,41
54,56,46,48
90,105,110,99,32,40,109,103,47,107,103,41
50,52,50,46,48
66,105,111,116,105,110,101,32,40,109,103,47,107,103,41
51,46,49,51
67,104,111,108,105,110,101,32,40,109,103,47,107,103,41
49,54,48,48,46,48
65,99,105,100,101,32,102,111,108,105,113,117,101,32,40,109,103,47,107,103,41
49,51,46,57
86,105,116,97,109,105,110,101,32,65,32,40,85,73,47,107,103,41
51,50,48,48,48,46,48
86,105,116,97,109,105,110,101,32,66,49,32,84,104,105,97,109,105,110,101,32,40,109,103,47,107,103,41
50,55,46,53
86,105,116,97,109,105,110,101,32,66,50,32,82,105,98,111,102,108,97,118,105,110,101,32,40,109,103,47,107,103,41
52,57,46,54
86,105,116,97,109,105,110,101,32,66,51,32,78,105,97,99,105,110,101,32,40,109,103,47,107,103,41
52,57,48,46,48
86,105,116,97,109,105,110,101,32,66,53,32,65,99,105,100,101,32,112,97,110,116,111,116,104,195,169,110,105,113,117,101,32,40,109,103,47,107,103,41
49,52,55,46,56
86,105,116,97,109,105,110,101,32,66,54,32,80,121,114,105,100,111,120,105,110,101,32,40,109,103,47,107,103,41
55,55,46,49
86,105,116,97,109,105,110,101,32,67,32,40,109,103,47,107,103,41
50,48,48,46,48
86,105,116,97,109,105,110,101,32,68,51,32,40,85,73,47,107,103,41
56,48,48,46,48
86,105,116,97,109,105,110,101,32,69,32,40,109,103,47,107,103,41
54,48,48,46,48
65,114,103,105,110,105,110,101,32,40,37,41
49,46,53,51
76,117,116,195,169,105,110,101,32,40,109,103,47,107,103,41
53,46,48
77,195,169,116,104,105,111,110,105,110,101,32,67,121,115,116,105,110,101,32,40,37,41
49,46,49,56
84,97,117,114,105,110,101,32,40,109,103,47,107,103,41
50,57,48,48,46,48
66,101,97,103,108,101,32,65,100,117,108,116
66,101,97,103,108,101,32,97,100,117,108,116,101,44,32,195,160,32,112,97,114,116,105,114,32,100,101,32,49,50,32,109,111,105,115
65,99,105,100,101,32,97,114,97,99,104,105,100,111,110,105,113,117,101,32,40,37,41
48,46,48,55
67,101,110,100,114,101,115,32,98,114,117,116,101,115,32,40,37,41
54,46,49
66,105,111,116,105,110,101,32,40,109,103,47,107,103,41
50,46,57,50
70,105,98,114,101,115,32,97,108,105,109,101,110,116,97,105,114,101,115,32,40,37,41
49,49,46,48
68,76,45,109,195,169,116,104,105,111,110,105,110,101,32,40,37,41
48,46,53
69,80,65,47,68,72,65,32,40,37,41
48,46,51
77,97,116,105,195,168,114,101,32,103,114,97,115,115,101,32,40,37,41
49,50,46,48
67,101,108,108,117,108,111,115,101,32,98,114,117,116,101,32,40,37,41
51,46,55
67,104,108,111,114,117,114,101,32,100,101,32,103,108,117,99,111,115,97,109,105,110,101,32,40,109,103,47,107,103,41
52,57,53,46,48
71,108,117,99,111,115,97,109,105,110,101,32,112,108,117,115,32,99,104,111,110,100,114,111,195,175,116,105,110,101,32,40,109,103,47,107,103,41
53,48,48,46,48
76,45,99,97,114,110,105,116,105,110,101,32,40,109,103,47,107,103,41
49,48,48,46,48
65,99,105,100,101,32,108,105,110,111,108,195,169,105,113,117,101,32,40,37,41
50,46,52,50
76,117,116,195,169,105,110,101,32,40,109,103,47,107,103,41
53,46,48
69,110,101,114,103,105,101,32,109,195,169,116,97,98,111,108,105,115,97,98,108,101,32,40,99,97,108,99,117,108,195,169,101,32,115,101,108,111,110,32,78,82,67,56,53,41,32,40,107,99,97,108,47,107,103,41
51,52,50,53,46,48
69,110,101,114,103,105,101,32,109,195,169,116,97,98,111,108,105,115,97,98,108,101,32,40,109,101,115,117,114,195,169,101,41,32,40,107,99,97,108,47,107,103,41
51,53,56,52,46,48
77,195,169,116,104,105,111,110,105,110,101,32,67,121,115,116,105,110,101,32,40,37,41
48,46,57,50
72,117,109,105,100,105,116,195,169,32,40,37,41
57,46,53
69,120,116,114,97,105,116,32,110,111,110,32,97,122,111,116,195,169,32,40,37,41
52,49,46,55
79,109,195,169,103,97,32,51,32,40,37,41
48,46,54,52
79,109,195,169,103,97,32,54,32,40,37,41
50,46,54,54
80,104,111,115,112,104,111,114,101,32,40,37,41
48,46,55
80,114,111,116,195,169,105,110,101,32,98,114,117,116,101,32,40,37,41
50,55,46,48
65,109,105,100,111,110,32,40,37,41
51,52,46,52
84,97,117,114,105,110,101,32,40,109,103,47,107,103,41
51,53,48,48,46,48
86,105,116,97,109,105,110,101,32,65,32,40,85,73,47,107,103,41
50,57,48,48,48,46,48
86,105,116,97,109,105,110,101,32,67,32,40,109,103,47,107,103,41
51,48,48,46,48
86,105,116,97,109,105,110,101,32,69,32,40,109,103,47,107,103,41
54,48,48,46,48
67,97,108,99,105,117,109,32,40,37,41
48,46,57
80,111,108,121,112,104,195,169,110,111,108,115,32,100,101,32,116,104,195,169,32,118,101,114,116,32,101,116,32,100,101,32,114,97,105,115,105,110,115,32,40,109,103,47,107,103,41
49,53,48,46,48
67,104,108,111,114,101,32,40,37,41
48,46,54,51
67,117,105,118,114,101,32,40,109,103,47,107,103,41
49,53,46,48
73,111,100,101,32,40,109,103,47,107,103,41
52,46,56
70,101,114,32,40,109,103,47,107,103,41
50,49,50,46,48
77,97,103,110,195,169,115,105,117,109,32,40,37,41
48,46,48,56
77,97,110,103,97,110,195,168,115,101,32,40,109,103,47,107,103,41
55,49,46,48
80,111,116,97,115,115,105,117,109,32,40,37,41
48,46,55
83,195,169,108,195,169,110,105,117,109,32,40,109,103,47,107,103,41
48,46,50,57
83,111,100,105,117,109,32,40,37,41
48,46,52
90,105,110,99,32,40,109,103,47,107,103,41
50,48,49,46,48
67,104,111,108,105,110,101,32,40,109,103,47,107,103,41
50,50,48,48,46,48
65,99,105,100,101,32,102,111,108,105,113,117,101,32,40,109,103,47,107,103,41
49,50,46,57
86,105,116,97,109,105,110,101,32,66,49,32,84,104,105,97,109,105,110,101,32,40,109,103,47,107,103,41
50,53,46,55
48,46,49,51
86,105,116,97,109,105,110,101,32,66,50,32,82,105,98,111,102,108,97,118,105,110,101,32,40,109,103,47,107,103,41
52,54,46,50
86,105,116,97,109,105,110,101,32,66,51,32,78,105,97,99,105,110,101,32,40,109,103,47,107,103,41
52,53,56,46,55
86,105,116,97,109,105,110,101,32,66,53,32,65,99,105,100,101,32,112,97,110,116,111,116,104,195,169,110,105,113,117,101,32,40,109,103,47,107,103,41
49,51,55,46,57
86,105,116,97,109,105,110,101,32,66,54,32,80,121,114,105,100,111,120,105,110,101,32,40,109,103,47,107,103,41
55,50,46,48
86,105,116,97,109,105,110,101,32,68,51,32,40,85,73,47,107,103,41
56,48,48,46,48
65,114,103,105,110,105,110,101,32,40,37,41
49,46,53,50
76,45,108,121,115,105,110,101,32,40,37,41
49,46,48,56
74,97,99,107,32,82,117,115,115,101,108,108,32,65,100,117,108,116
7
53,46,57
66,105,111,116,105,110,101,32,40,109,103,47,107,103,41

by:

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

我收到错误:

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

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

这是我尝试的第二件事:通过转向ASCI来摆脱那些é'èàù%和其他麻烦制造者:

strip

结果不错但不够:

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," ", , , , , , , , 时。生成的CSV为空。

现在只有strip产生错误:

  

UnicodeEncodeError:'ascii'编解码器无法编码字符'\ xe8'   位置8:序数不在范围内(128)

我阅读并尝试了大部分stack-topic

ASCII方式不是很好,因为我失去了信息。我确实需要它们。

如何将我的列表正确放入此FB.csv?

2 个答案:

答案 0 :(得分:0)

在代码开始时使用这些说明测试代码:

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

你应该看看here

答案 1 :(得分:0)

看起来你正在使用Python 3. 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

中指定
>>> import csv
>>> with open('test.csv','w',encoding='utf8',newline='') as f:
...  w=csv.writer(f)
...  rows=['\xe8abc,def','\xe8def,ghi']
...  w.writerows(rows)
...
>>>

注意:代码页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