新手问题了。 我从几个URL获取信息对,从BS4和URLlib获取 我创建了一对列表列表。 例如:一对夫妇(名字,价值) 例如:一个URL的对列表 例如:能够对字典中所有内容进行分类的对列表。
应该如下所示的对列表列表:
List_of_list_of_pairs = [ [ [a,1],[c,2],[e,3] ], [ [g,4],[r,6] ... ], ... ]
以下是我正在使用的代码:
from bs4 import BeautifulSoup
import csv
import urllib.request
from collections import *
def parse_csv(content, delimiter = ';'): ##We use here ";" to parse CSV because of the European way of dealing with excel-csv
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_of_list_of_pairs=[]
List_of_pairs=[]
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_0 = BeautifulSoup(page.read(),"html.parser")
restricted_webpage= soup_0.find( "div", {"id":"ingredients"} )
readable_restricted=str(restricted_webpage)
soup=BeautifulSoup(readable_restricted,"html.parser")
trs = soup.find_all('tr')
for tr in trs:
tds = tr.find_all("td")
try: #we are using "try" because the table is not well formatted. This allows the program to continue after encountering an error.
Nutriments = str(tds[0].get_text().strip())
print(Nutriments)
# This structure $isolate the item by its column in the table and converts it into a string.
Quantity = str(tds[1].get_text().strip())
print(Quantity)
Pair=[Nutriments,Quantity]
List_of_pairs.append(Pair)
except:
print ("bad tr string")
continue #This tells the computer to move on to the next item after it encounters an error
List_of_list_of_pairs.append([List_of_pairs])
print(List_of_list_of_pairs)
dico = defaultdict(list)
for n,list_of_pairs in enumerate(List_of_list_of_pairs):
for i,pairs in enumerate(list_of_pairs):
if (len(pairs) == 2):
cle = pairs[0]
val = pairs[1]
while (len(dico[cle]) < n):
dico[cle].append(-1)
dico[cle].append(val)
for cle in dico:
while (len(dico[cle]) < len(List_of_list_of_pairs)):
dico[cle].append(-1)
print(dico.items())
问题:dico.items()
返回空的内容。
我担心我的配对清单列表没有被很好地阅读。 也许是建造得很糟糕。 有什么吸引我的注意力。列表开头和结尾的“[”和“]”数。我想这对很多人来说都是这样的。
这是print(list_of_list_pairs)
:
[[['Cendres brutes (%)', '7.4'], ['Cellulose brute (%)', '1.6'], ['Fibres alimentaires (%)', '6.6'],..., [[['Cendres brutes (%)', '7.4'], ['Cellulose brute (%)', '1.6'], ['Fibres alimentaires (%)', '6.6'], ['Matière grasse (%)', '16.0'], ['Acide linoléique (%)', '3.1'], ['Energie métabolisable (calculée selon NRC85) (kcal/kg)', '3652.5'], ['Energie métabolisable (mesurée) (kcal/kg)', '3900.0'], ['Humidité (%)', '9.5'] ['Arginine (%)', '1.54'], ['L-lysine (%)', '1.15']]], [[['Cendres brutes (%)', '7.4']..
我认为这是错误的。像Cendres brutes (%)
这样的名称在此列表中只能出现3次(因为每个URL中都有一个)。我觉得有些不对劲。
但这并不能解释为什么我的指针是空的。
或者也许是相关的。
欢迎任何帮助!
更新:我要做的是:从几个URL中废弃的无组织信息对,获得一个看似如下的有组织的csv:
NameKey ValURL1 ValURL2 ValURL3
a 2.4 5.6 -1
b 1.2 4.2 -1
c -1 -1 val
....
a,b,c ......你可以读到像“Cendres Brutes”,“AcideLinoléique”......
更新:print(List_of_list_of_pairs)
返回:
[[[['Cendres brutes (%)', '7.4'], ['Cellulose brute (%)', '1.6'], ['Fibres alimentaires (%)', '6.6'], ['Matière grasse (%)', '16.0'], ['Acide linoléique (%)', '3.1'], ['Energie métabolisable (calculée selon NRC85) (kcal/kg)', '3652.5'], ['Energie métabolisable (mesurée) (kcal/kg)', '3900.0'], ['Humidité (%)', '9.5'], ['Extrait non azoté (%)', '40.5'], ['Oméga 6 (%)', '3.18'], ['Protéine brute (%)', '25.0'], ['Amidon (%)', '35.5'], ['Chlore (%)', '1.43'], ['Cuivre (mg/kg)', '15.0'], ['Iode (mg/kg)', '2.9'], ['Fer (mg/kg)', '167.0'], ['Manganèse (mg/kg)', '68.0'], ['Zinc (mg/kg)', '242.0'], ['Biotine (mg/kg)', '3.13'], ['Choline (mg/kg)', '1600.0'], ['Acide folique (mg/kg)', '13.9'], ['Vitamine A (UI/kg)', '32000.0'], ['Vitamine B1 Thiamine (mg/kg)', '27.5'], ['Vitamine B2 Riboflavine (mg/kg)', '49.6'], ['Vitamine B3 Niacine (mg/kg)', '490.0'], ['Vitamine B5 Acide pantothénique (mg/kg)', '147.8'], ['Vitamine B6 Pyridoxine (mg/kg)', '77.1'], ['Vitamine C (mg/kg)', '200.0'], ['Vitamine D3 (UI/kg)', '800.0'], ['Vitamine E (mg/kg)', '600.0'], ['Arginine (%)', '1.53'], ['Lutéine (mg/kg)', '5.0'], ['Méthionine Cystine (%)', '1.18'], ['Taurine (mg/kg)', '2900.0'], ['Acide arachidonique (%)', '0.07'], ['Cendres brutes (%)', '6.1'], ['Biotine (mg/kg)', '2.92'], ['Fibres alimentaires (%)', '11.0'], ['DL-méthionine (%)', '0.5'], ['EPA/DHA (%)', '0.3'], ['Matière grasse (%)', '12.0'], ['Cellulose brute (%)', '3.7'], ['Chlorure de glucosamine (mg/kg)', '495.0'], ['Glucosamine plus chondroïtine (mg/kg)', '500.0'], ['L-carnitine (mg/kg)', '100.0'], ['Acide linoléique (%)', '2.42'], ['Lutéine (mg/kg)', '5.0'], ['Energie métabolisable (calculée selon NRC85) (kcal/kg)', '3425.0'], ['Energie métabolisable (mesurée) (kcal/kg)', '3584.0'], ['Méthionine Cystine (%)', '0.92'], ['Humidité (%)', '9.5'], ['Extrait non azoté (%)', '41.7'], ['Oméga 3 (%)', '0.64'], ['Oméga 6 (%)', '2.66'], ['Phosphore (%)', '0.7'], ['Protéine brute (%)', '27.0'], ['Amidon (%)', '34.4'], ['Taurine (mg/kg)', '3500.0'], ['Vitamine A (UI/kg)', '29000.0'], ['Vitamine C (mg/kg)', '300.0'], ['Vitamine E (mg/kg)', '600.0'], ['Calcium (%)', '0.9'], ['Polyphénols de thé vert et de raisins (mg/kg)', '150.0'], ['Chlore (%)', '0.63'], ['Cuivre (mg/kg)', '15.0'], ['Iode (mg/kg)', '4.8'], ['Fer (mg/kg)', '212.0'], ['Magnésium (%)', '0.08'], ['Manganèse (mg/kg)', '71.0'], ['Potassium (%)', '0.7'], ['Sélénium (mg/kg)', '0.29'], ['Sodium (%)', '0.4'], ['Zinc (mg/kg)', '201.0'], ['Choline (mg/kg)', '2200.0'], ['Acide folique (mg/kg)', '12.9'], ['Vitamine B1 Thiamine (mg/kg)', '25.7'], ['Vitamine B12 Cyanocobalamine (mg/kg)', '0.13'], ['Vitamine B2 Riboflavine (mg/kg)', '46.2'], ['Vitamine B3 Niacine (mg/kg)', '458.7'], ['Vitamine B5 Acide pantothénique (mg/kg)', '137.9'], ['Vitamine B6 Pyridoxine (mg/kg)', '72.0'], ['Vitamine D3 (UI/kg)', '800.0'], ['Arginine (%)', '1.52'], ['L-lysine (%)', '1.08'], ['Acide arachidonique (%)', '0.07'], ['Cendres brutes (%)', '5.9'], ['Biotine (mg/kg)', '3.07'], ['Fibres alimentaires (%)', '7.0'], ['DL-méthionine (%)', '0.65'], ['EPA/DHA (%)', '0.3'], ['Matière grasse (%)', '16.0'], ['Cellulose brute (%)', '1.3'], ['Chlorure de glucosamine (mg/kg)', '495.0'], ['Glucosamine plus chondroïtine (mg/kg)', '500.0'], ['L-carnitine (mg/kg)', '150.0'], ['Acide linoléique (%)', '2.88'], ['Lutéine (mg/kg)', '5.0'], ['Energie métabolisable (calculée selon NRC85) (kcal/kg)', '3716.0'], ['Energie métabolisable (mesurée) (kcal/kg)', '3964.0'], ['Méthionine Cystine (%)', '1.15'], ['Humidité (%)', '9.5'], ['Extrait non azoté (%)', '37.3'], ['Oméga 3 (%)', '0.63'], ['Oméga 6 (%)', '3.13'], ['Phosphore (%)', '0.6'], ['Protéine brute (%)', '30.0'], ['Amidon (%)', '31.6'], ['Taurine (mg/kg)', '2000.0'], ['Vitamine A (UI/kg)', '31000.0'], ['Vitamine C (mg/kg)', '300.0'], ['Vitamine E (mg/kg)', '600.0'], ['Calcium (%)', '0.89'], ['Polyphénols de thé vert et de raisins (mg/kg)', '150.0'], ['Chlore (%)', '0.68'], ['Cuivre (mg/kg)', '15.0'], ['Iode (mg/kg)', '4.8'], ['Fer (mg/kg)', '200.0'], ['Magnésium (%)', '0.12'], ['Manganèse (mg/kg)', '73.0'], ['Potassium (%)', '0.65'], ['Sélénium (mg/kg)', '0.27'], ['Sodium (%)', '0.4'], ['Zinc (mg/kg)', '205.0'], ['Choline (mg/kg)', '2400.0'], ['Acide folique (mg/kg)', '13.6'], ['Vitamine B1 Thiamine (mg/kg)', '26.9'], ['Vitamine B12 Cyanocobalamine (mg/kg)', '0.13'], ['Vitamine B2 Riboflavine (mg/kg)', '48.5'], ['Vitamine B3 Niacine (mg/kg)', '481.9'], ['Vitamine B5 Acide pantothénique (mg/kg)', '144.9'], ['Vitamine B6 Pyridoxine (mg/kg)', '75.6'], ['Vitamine D3 (UI/kg)', '800.0'], ['Arginine (%)', '1.54'], ['L-lysine (%)', '1.15']]]]
更新2:print(List_of_list_of_pairs[0][0])
返回:
[['Cendres brutes (%)', '7.4'], ['Cellulose brute (%)', '1.6'], ['Fibres alimentaires (%)', '6.6'], ['Matière grasse (%)', '16.0'], ['Acide linoléique (%)', '3.1'], ['Energie métabolisable (calculée selon NRC85) (kcal/kg)', '3652.5'], ['Energie métabolisable (mesurée) (kcal/kg)', '3900.0'], ['Humidité (%)', '9.5'], ['Extrait non azoté (%)', '40.5'], ['Oméga 6 (%)', '3.18'], ['Protéine brute (%)', '25.0'], ['Amidon (%)', '35.5'], ['Chlore (%)', '1.43'], ['Cuivre (mg/kg)', '15.0'], ['Iode (mg/kg)', '2.9'], ['Fer (mg/kg)', '167.0'], ['Manganèse (mg/kg)', '68.0'], ['Zinc (mg/kg)', '242.0'], ['Biotine (mg/kg)', '3.13'], ['Choline (mg/kg)', '1600.0'], ['Acide folique (mg/kg)', '13.9'], ['Vitamine A (UI/kg)', '32000.0'], ['Vitamine B1 Thiamine (mg/kg)', '27.5'], ['Vitamine B2 Riboflavine (mg/kg)', '49.6'], ['Vitamine B3 Niacine (mg/kg)', '490.0'], ['Vitamine B5 Acide pantothénique (mg/kg)', '147.8'], ['Vitamine B6 Pyridoxine (mg/kg)', '77.1'], ['Vitamine C (mg/kg)', '200.0'], ['Vitamine D3 (UI/kg)', '800.0'], ['Vitamine E (mg/kg)', '600.0'], ['Arginine (%)', '1.53'], ['Lutéine (mg/kg)', '5.0'], ['Méthionine Cystine (%)', '1.18'], ['Taurine (mg/kg)', '2900.0'], ['Acide arachidonique (%)', '0.07'], ['Cendres brutes (%)', '6.1'], ['Biotine (mg/kg)', '2.92'], ['Fibres alimentaires (%)', '11.0'], ['DL-méthionine (%)', '0.5'], ['EPA/DHA (%)', '0.3'], ['Matière grasse (%)', '12.0'], ['Cellulose brute (%)', '3.7'], ['Chlorure de glucosamine (mg/kg)', '495.0'], ['Glucosamine plus chondroïtine (mg/kg)', '500.0'], ['L-carnitine (mg/kg)', '100.0'], ['Acide linoléique (%)', '2.42'], ['Lutéine (mg/kg)', '5.0'], ['Energie métabolisable (calculée selon NRC85) (kcal/kg)', '3425.0'], ['Energie métabolisable (mesurée) (kcal/kg)', '3584.0'], ['Méthionine Cystine (%)', '0.92'], ['Humidité (%)', '9.5'], ['Extrait non azoté (%)', '41.7'], ['Oméga 3 (%)', '0.64'], ['Oméga 6 (%)', '2.66'], ['Phosphore (%)', '0.7'], ['Protéine brute (%)', '27.0'], ['Amidon (%)', '34.4'], ['Taurine (mg/kg)', '3500.0'], ['Vitamine A (UI/kg)', '29000.0'], ['Vitamine C (mg/kg)', '300.0'], ['Vitamine E (mg/kg)', '600.0'], ['Calcium (%)', '0.9'], ['Polyphénols de thé vert et de raisins (mg/kg)', '150.0'], ['Chlore (%)', '0.63'], ['Cuivre (mg/kg)', '15.0'], ['Iode (mg/kg)', '4.8'], ['Fer (mg/kg)', '212.0'], ['Magnésium (%)', '0.08'], ['Manganèse (mg/kg)', '71.0'], ['Potassium (%)', '0.7'], ['Sélénium (mg/kg)', '0.29'], ['Sodium (%)', '0.4'], ['Zinc (mg/kg)', '201.0'], ['Choline (mg/kg)', '2200.0'], ['Acide folique (mg/kg)', '12.9'], ['Vitamine B1 Thiamine (mg/kg)', '25.7'], ['Vitamine B12 Cyanocobalamine (mg/kg)', '0.13'], ['Vitamine B2 Riboflavine (mg/kg)', '46.2'], ['Vitamine B3 Niacine (mg/kg)', '458.7'], ['Vitamine B5 Acide pantothénique (mg/kg)', '137.9'], ['Vitamine B6 Pyridoxine (mg/kg)', '72.0'], ['Vitamine D3 (UI/kg)', '800.0'], ['Arginine (%)', '1.52'], ['L-lysine (%)', '1.08'], ['Acide arachidonique (%)', '0.07'], ['Cendres brutes (%)', '5.9'], ['Biotine (mg/kg)', '3.07'], ['Fibres alimentaires (%)', '7.0'], ['DL-méthionine (%)', '0.65'], ['EPA/DHA (%)', '0.3'], ['Matière grasse (%)', '16.0'], ['Cellulose brute (%)', '1.3'], ['Chlorure de glucosamine (mg/kg)', '495.0'], ['Glucosamine plus chondroïtine (mg/kg)', '500.0'], ['L-carnitine (mg/kg)', '150.0'], ['Acide linoléique (%)', '2.88'], ['Lutéine (mg/kg)', '5.0'], ['Energie métabolisable (calculée selon NRC85) (kcal/kg)', '3716.0'], ['Energie métabolisable (mesurée) (kcal/kg)', '3964.0'], ['Méthionine Cystine (%)', '1.15'], ['Humidité (%)', '9.5'], ['Extrait non azoté (%)', '37.3'], ['Oméga 3 (%)', '0.63'], ['Oméga 6 (%)', '3.13'], ['Phosphore (%)', '0.6'], ['Protéine brute (%)', '30.0'], ['Amidon (%)', '31.6'], ['Taurine (mg/kg)', '2000.0'], ['Vitamine A (UI/kg)', '31000.0'], ['Vitamine C (mg/kg)', '300.0'], ['Vitamine E (mg/kg)', '600.0'], ['Calcium (%)', '0.89'], ['Polyphénols de thé vert et de raisins (mg/kg)', '150.0'], ['Chlore (%)', '0.68'], ['Cuivre (mg/kg)', '15.0'], ['Iode (mg/kg)', '4.8'], ['Fer (mg/kg)', '200.0'], ['Magnésium (%)', '0.12'], ['Manganèse (mg/kg)', '73.0'], ['Potassium (%)', '0.65'], ['Sélénium (mg/kg)', '0.27'], ['Sodium (%)', '0.4'], ['Zinc (mg/kg)', '205.0'], ['Choline (mg/kg)', '2400.0'], ['Acide folique (mg/kg)', '13.6'], ['Vitamine B1 Thiamine (mg/kg)', '26.9'], ['Vitamine B12 Cyanocobalamine (mg/kg)', '0.13'], ['Vitamine B2 Riboflavine (mg/kg)', '48.5'], ['Vitamine B3 Niacine (mg/kg)', '481.9'], ['Vitamine B5 Acide pantothénique (mg/kg)', '144.9'], ['Vitamine B6 Pyridoxine (mg/kg)', '75.6'], ['Vitamine D3 (UI/kg)', '800.0'], ['Arginine (%)', '1.54'], ['L-lysine (%)', '1.15']]
似乎list_of_list_of_paris构建得很糟糕。我不明白为什么。
答案 0 :(得分:-1)
由于你有一对对象列表,List_of_list_of_pairs[n]
仍然是对的列表,它不会是对。
但是你的代码假设List_of_list_of_pairs[n]
是对,它不是。你应该迭代第一个List_of_list_of_pairs
然后迭代它中的元素(它们是对的列表),然后尝试将它们添加到字典中。
示例 -
for n,list_of_pairs in enumerate(List_of_list_of_pairs):
for i,pairs in enumerate(list_of_pairs):
if (len(pairs) == 2):
cle = pairs[0]
val = pairs[1]
#Do the rest of your logic.
此外,你有这个循环 -
for cle in dico:
while (len(dico[cle]) < len(s)):
dico[cle].append(-1)
但我没有看到s
被定义在任何地方,s
应该是什么?