【问题标题】:How to create this kind of dictionary?如何创建这种字典?
【发布时间】:2015-07-22 22:37:30
【问题描述】:

我正在从几个 URL 表中抓取数据。我的代码允许我得到这样的csv(使用分号作为昏迷......欧洲)

a;1  -------- 1st URL
b;2
c;3
d;4
e;5
a;7 ---------- 2nd URL
b;3
c;5
d;8
e;9
a;9 ---------- 3rd URL
b;3
y;5

---URL 不是 CSV 的一部分。它只是向您显示 URL1 的数据从哪里开始,等等。
我从来不知道一个 URL 将包含多少字段以及他将包含哪些字段。 我想得到一个看起来像这样组织良好的 CSV(带有示例):

Name ; 1stUrl ;2ndURl ;3rd Url
a;1;7;9
b;2;3;3
c;3;5;ø
d;4;8;ø
e;5;9;ø
y;ø;ø;5

我不太关心 a,b,c,d 的顺序。这些字段可以按照他们最喜欢的顺序排列。

问题是我有两个问题: - 当代码遇到新字段时,它必须将其添加到字段列表中('y' 字段的示例)。 - 代码必须留有空白/真空空间;ø;当该字段的 URL 中没有键时。

我尝试了一些不好的东西,但显然不是这样。即使在概念上,我也不在那里。

from collections import *
import csv

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


s =parse_csv(open('raw.csv','rU', encoding='utf8').read())
print(len(s))
dic = defaultdict(list)
for n in range(0,len(s)):
    if (len(s[n]) == 2):
        key = s[n][0]
        val = s[n][1]
        print(key)
        print(val)


writer = csv.writer(open('dict.csv', 'w',encoding='utf8'), delimiter=';')
for key, value in dico.items():
   writer.writerow([key, value])

你怎么看?
任何帮助将不胜感激:)!

【问题讨论】:

    标签: python csv dictionary


    【解决方案1】:

    您可以将list 对象作为dict 的值。所以很简单:

    content = """a;1
    b;2
    c;3
    d;4
    e;5
    a;7
    b;3
    c;5
    d;8
    e;9
    a;9
    b;3
    y;5"""
    
    csv_data = []
    for line in content.split('\n'):
        csv_data.append( [x.strip() for x in line.split(';')] )
    
    s = csv_data
    dic = {}
    for n in range(0,len(s)):
        if (len(s[n]) == 2):
            key = s[n][0]
            val = s[n][1]
            if key not in dic:
                dic[key] = []
            dic[key].append(val)
    

    现在dic 是:

    {'a': ['1', '7', '9'],
     'b': ['2', '3', '3'],
     'c': ['3', '5'],
     'd': ['4', '8'],
     'e': ['5', '9'],
     'y': ['5']}
    

    我猜这就是你想要的。

    附带说明,我强烈建议您永远不要使用from package_xx import *,因为它会使您的环境变得混乱。

    【讨论】:

    • 感谢您的明确答复。但我不确定它是否能完全按照我想要的方式工作。我不是很清楚,对此我很抱歉。实际上,在您的最终 dic 中,我们没有比“y”字段中的信息,5 来自第三个 URL。事实上,它被放置在第一个位置,就在 a1 的下方,它来自第一个 URL。
    • 好的,你怎么知道一个 url 什么时候完成?是否有一个“停止代码”来知道一个 url 已经完成而另一个 url 开始?
    • 没有。那没有。现在有一个配对列表,可以让我知道 URL 何时停止。现在可以了。我会更新一个有效的答案。
    【解决方案2】:
    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)) :
        List_of_pairs = []
    
        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('ND')
                 dico[cle].append(val)
    for cle in dico:
        while (len(dico[cle]) < n):
                dico[cle].append('ND')
    
    
    import csv
    with open("dict2csv.csv", 'w',encoding='utf8') as outfile:
       csv_writer = csv.writer(outfile, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL,)
    
       for k,v in dico.items():
           csv_writer.writerow([k] + v)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-07
      • 2017-12-17
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      相关资源
      最近更新 更多