简体   繁体   中英

Python AttributeError: 'dict' object has no attribute 'startswith' when 'dict' in a list

I am working on extracting hash tags from data I have in json files that are of list type. This works for some of my files but for others that contain a 'dict' in the list it fails. Is there anyway I can modify my code to accommodate for this? I have included an example where it works and an example where it doesn't.

file_name = 'twitter1.json'
with open(file_path + file_name) as json_file:
    data = json.load(json_file)
data
['http://b8nicktof280.com/skoex/po2.php?l=deof', 
'http://dwillow100bc.com/skoex/po2.php?l=deof',
'#ursnif', '#malspam']

type(data)
list

#Extract the tags for use in api post assignment
tags = [tag for tag in data if tag.startswith('#')]
tags
['#ursnif','#malspam']

This extracts the tags with no problem.

But for the next example the data type is a list as well, but has {} in it causing an error: AttributeError: 'dict' object has no attribute 'startswith'

file_name = 'twitter2.json'
with open(file_path + file_name) as json_file:
    data = json.load(json_file)
data
['t.co', '', '103.126.6.93', '#twitter', {'Address': '103.126.6.93'}]

type(data)
list

#Extract the tags for use in api post assignment
tags = [tag for tag in data if tag.startswith('#')]
AttributeError: 'dict' object has no attribute 'startswith'

最简单的解决方案是忽略data中不是字符串的任何项目:

tags = [tag for tag in data if isinstance(tag, str) and tag.startswith('#')]

Check for the datatype of tag in the last list comprehension and append it accordingly.

tags = [tag if isinstance(tag, list) else list(tag.values())[0] for tag in data]    

Then use startswith() in tags list:

li = [tag for tag in tags if tag.startswith(‘#’)].   

For tags I assume a single value in dictionary, if that isn't the case, we can make a string after joining all dict.values()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM