如何遍历具有单个值的字典的文本?

时间:2020-04-16 00:59:12

标签: python dictionary

我在字典中包含一个单一值时遇到麻烦。这个单一值是采用以下方式设置的文本格式:

    dict_values([{'top':                       query  value
0      the urban dictionary    100
1                definition     50
2    urban dictionary words     12
3    urban dictionary names     10
4     word urban dictionary     10
5          urban outfitters      9
6     thot urban dictionary      8
7      dog urban dictionary      6
8      cap urban dictionary      6
9     urban dictionary yeet      6
10                     yeet      6
11     top urban dictionary      5
12    stan urban dictionary      4
13  urban dictionary boomer      4
14                   boomer      4
15    fomo urban dictionary      4
16    urban dictionary drip      4
17     smh urban dictionary      4
18                      smh      4
19                urban air      3
20     tea urban dictionary      3
21   green urban dictionary      3
22     bet urban dictionary      3
23                     vsco      3
24    goat urban dictionary      3

我想将该字典转换为具有三列的数据框-索引,查询和流行度。知道怎么做吗?

这是获取字典的代码:

!pip install pytrends
import pandas as pd                        
from pytrends.request import TrendReq
pytrend = TrendReq()
ytrend.build_payload(kw_list=['urban dictionary'],timeframe='2019-01-01 2019-12-31')
related_queries = pytrend.related_queries()
related_queries.values()

干杯

编辑:输出必须是数据框,而不是字典

2 个答案:

答案 0 :(得分:1)

data2 = dict(re.findall("\s*([a-zA-Z ]+[a-zA-Z])\s{2,}(\d+)",data['top']))

我想您会很近(结果中的数字是字符串...)

顺便说一句,尽管我会冒险猜测您的data ['top']实际上是一个数据框而不是一个字符串(基于打印的表示形式)

在这种情况下,您可以执行data['top']['query']并且将打印整个列,或者执行data['top'][data['top']['query'] == 'urban outfitters']仅查看匹配的行

答案 1 :(得分:1)

conda install xarray=0.15.0

from typing import List, NamedTuple class Entry(NamedTuple): index: int query: str popularity: int def format_dictionary_text(text: str) -> List[Entry]: entries: List[Entry] = [] for line in text.split("\n"): tokens = line.split() if len(tokens) < 3: continue # "query value" garbage at top? entries.append(Entry( int(tokens[0]), " ".join(tokens[1:-1]), int(tokens[-1]) )) return entries text = """ query value 0 the urban dictionary 100 1 definition 50 2 urban dictionary words 12 3 urban dictionary names 10 4 word urban dictionary 10 5 urban outfitters 9 6 thot urban dictionary 8 7 dog urban dictionary 6 8 cap urban dictionary 6 9 urban dictionary yeet 6 10 yeet 6 11 top urban dictionary 5 12 stan urban dictionary 4 13 urban dictionary boomer 4 14 boomer 4 15 fomo urban dictionary 4 16 urban dictionary drip 4 17 smh urban dictionary 4 18 smh 4 19 urban air 3 20 tea urban dictionary 3 21 green urban dictionary 3 22 bet urban dictionary 3 23 vsco 3 24 goat urban dictionary 3 """ print(format_dictionary_text(text)) 列表对我来说似乎是最方便的表示形式,但是如果您愿意,可以直接将其转换为某种字典。