2015-07-20 50 views
1

我需要將矢量/列表的內容放入CSV中。 顯然,我遇到了「python編碼問題」的麻煩。從Urlib寫入CSV並正確管理編碼

這裏是我們taklin'關於代碼:

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 

這意味着我將不得不解碼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)

我讀,並試圖極本stack-topic

的ASCII方式是不是很好,因爲我失去了信息。我確實需要它們。

我該如何正確地將我的列表放到這個FB.csv中?

+0

噢噢噢野應無限循環的代碼。我猜應該更短。 – juanmajmjr

+0

它在哪裏無限循環? – BoobaGump

+0

[unicodecsv](https://github.com/jdunck/python-unicodecsv)呢? – NightShadeQueen

回答

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 
+0

謝謝你的明確答案。儘管如此,我仍然無法完成工作。我編輯我的帖子與url.csv,如果你們有些人想嘗試你所建議的更正。 – BoobaGump

+0

對不起。它有效,但不是我期待的方式。事情是我在輸出的每個角色之間保持着那些「,」。當我在Excel中打開CSV時,它看起來像:h,e,l,l,o(下面一行)i,s,u,c,k,a,t,c,s,v。我不明白爲什麼。因爲當我'打印(行)'它正確安靜地出現在shell中。 – BoobaGump

+0

您仍在傳遞「字符串列表」而不是「字符串列表列表」,在第二個示例中給出了您的問題。 '['abc','def']'vs.'[['abc','def']]'。 ''作家'採取前者,而'作家'採取後者。 –

0

測試YOUT代碼在你的代碼的beggining這些指令:

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

你應該看一看here

+0

'setdefaultencoding'在Python 3中不再可用。而當我問我的IDLE什麼是默認編碼時,答案是UTF8。所以我想這不是必需的,是嗎? – BoobaGump