为什么我的字典是空的?

2024-09-30 02:33:06 发布

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

又是新手问题。 我从几个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中都有一个)。我觉得有点不对劲。 但这不能解释为什么我的dic是空的。 或者是相关的。你知道吗

欢迎任何帮助!你知道吗

更新:我想做的是:从几个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。。。当你读起来像是“圣人的畜生”,“酸亚麻油”。。。你知道吗

UPDATE: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']]

看来巴黎的建筑很糟糕。我不明白为什么。你知道吗


Tags: ofuidelistkgbrutepairsmg
1条回答
网友
1楼 · 发布于 2024-09-30 02:33:06

因为您有一个成对列表,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应该是什么?你知道吗

相关问题 更多 >

    热门问题