相关疑难解决方法(0)

创建字典Python列表

我想iframe从网页上获取所有内容.

码:

site = "http://" + url
f = urllib2.urlopen(site)
web_content =  f.read()

soup = BeautifulSoup(web_content)
info = {}
content = []
for iframe in soup.find_all('iframe'):
    info['src'] = iframe.get('src')
    info['height'] = iframe.get('height')
    info['width'] = iframe.get('width')
    content.append(info)
    print(info)       

pprint(content)
Run Code Online (Sandbox Code Playgroud)

结果print(info):

{'src': u'abc.com', 'width': u'0', 'height': u'0'}
{'src': u'xyz.com', 'width': u'0', 'height': u'0'}
{'src': u'http://www.detik.com', 'width': u'1000', 'height': u'600'}
Run Code Online (Sandbox Code Playgroud)

结果pprint(content):

[{'height': u'600', 'src': u'http://www.detik.com', 'width': u'1000'},
{'height': u'600', 'src': u'http://www.detik.com', 'width': u'1000'},
{'height': u'600', 'src': u'http://www.detik.com', …
Run Code Online (Sandbox Code Playgroud)

python dictionary list

28
推荐指数
3
解决办法
15万
查看次数

即使密钥存在,Pandas groupby 也会给出“keyError”

我是 Python 新手,对于我的一个项目,我需要将 csv 转换为嵌套 Json。在网上搜索,我发现pandas对这种情况很有帮助。我按照将CSV 数据转换为 Python 中的嵌套 JSON 中给出的方法进行操作 ,但我收到了 keyError 异常KeyError: 'state'

df info
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 3 columns):
country    4 non-null object
 state     4 non-null object
 city      4 non-null object
dtypes: object(3)
memory usage: 176.0+ bytes
None
Traceback (most recent call last):
  File "csvToJson.py", line 31, in <module>
    grouped = df.groupby(['country', 'state'])
  File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/pandas/core/generic.py", line 7632, in groupby
    observed=observed, **kwargs)
  File "/home/simarpreet/Envs/j/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line …
Run Code Online (Sandbox Code Playgroud)

python csv json pandas keyerror

9
推荐指数
1
解决办法
4万
查看次数

标签 统计

python ×2

csv ×1

dictionary ×1

json ×1

keyerror ×1

list ×1

pandas ×1