Python字典数组 - 每行一行

时间:2017-01-09 21:13:13

标签: python dictionary web-scraping

我有一个字典,每个数组都包含多个值。

upcthreadsmall.txt包含一系列UPC编号“000381001184”,“000381102935”,“000381003263”等。

使用下面的代码显示为:

000381002365 : ['$265.29', '$299.00', '$270.70', '$299.00']

我希望它显示为:

000381002365 : $265.29
000381002365 : $299.00
000381002365 : $270.70
000381002365 : $299.00

代码:

def th(ur):
    base = "https://www.slickguns.com/search/apachesolr_search/"+ur
    regex = '<td class="price-column">(.+?)</td>'
    pattern = re.compile(regex)
    htmltext = urllib.urlopen(base).read()
    results = re.findall(pattern, htmltext)
    try:
        gmap[ur] = results
    except:"got an error"

upclist = open("upcthreadsmall.txt").read()
upclist = upclist.replace(" ","").split(",")

threadlist = []

for u in upclist:
    t = Thread(target=th,args=(u,))
    t.start()
    threadlist.append(t)

for b in threadlist:
    b.join()


for key, upc in gmap.items():
    print(key)
    for attribute, value in gmap.items():
        print('{} : {}'.format(attribute, value)

1 个答案:

答案 0 :(得分:2)

只需在value上创建另一个循环,也许您想对属性进行排序:

for attribute, value in sorted(gmap.items()):
    for v in value:
        print('{} : {}'.format(attribute, v))