[英]Accessing dictionary elements in python
I working with some twitter data, I get the data by monitoring the twitter stream, then I save the results in a *.txt file 我处理一些Twitter数据,通过监视Twitter流获得数据,然后将结果保存在* .txt文件中
I´m trying to manipulate this txt file with python, for that I use the json.loads() instruction, with every line in the file where the twitter stream result was saved, in that way I got every file line as an json object. 我正在尝试使用python处理该txt文件,因为我使用json.loads()指令,并在其中保存了Twitter流结果的文件中的每一行,因此我将每一行文件都作为json对象。
for line in twitter_file
data = json.loads(line)
The json object (one json object for one file line) is loaded in a variable called "data" json对象(一个文件行对应一个json对象)被加载到名为“ data”的变量中
Everyting is working there, when I try to access an element in the "data" json object I can do it, for example, if I can see the place element, I can do it with data["place"], so I got this: 一切都在这里工作,当我尝试访问“数据” json对象中的元素时,我可以做到这一点,例如,如果可以看到place元素,则可以使用data [“ place”]来做到这一点,所以我得到了这个:
"place":{
"id":"00a961d91f76dde8",
"url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/00a961d91f76dde8.json",
"place_type":"city",
"name":"Capital - Corrientes",
"full_name":"Capital - Corrientes",
"country_code":"AR",
"country":"Argentina",
"contained_within":[]
}
I can access the "place" element, if I execute print data["place"] , I can see the text displayed above. 我可以访问“ place”元素,如果我执行print data [“ place”],我可以看到上面显示的文本。
The problem comes when I'm trying to access an specific key in place dictionary, 当我尝试访问位置字典中的特定键时,就会出现问题,
I tried to do it in this way 我试图用这种方式
data["place"]["place_type"]
I'm waiting to get the "city" value as result, but I can not do it, I get the following error: TypeError: "NoneType" object has no attribute '__getitem__'
我正在等待获取“城市”值作为结果,但是我做不到,出现以下错误:
TypeError: "NoneType" object has no attribute '__getitem__'
I also tried other ways to display the key-value pairs with the following: 我还尝试了其他方法来显示具有以下内容的键值对:
for key,value in data["data"].items()
print key
But I don't get the result 但我没有得到结果
I also tried with 我也尝试过
print data["place"].keys()[0]
To get printed the first element, but It doesn't work either. 要打印第一个元素,但是它也不起作用。 I got the following error message:
AttributeError: 'NoneType' object has no attribute 'keys'
我收到以下错误消息:
AttributeError: 'NoneType' object has no attribute 'keys'
It seems that my data["place"] is not being considered as a dictionary by Python, that's what I guess, but I'm not sure, I'm pretty new at Python, so any comment will be very helpful for me. 似乎我的data [“ place”]并没有被Python视为字典,这就是我的猜测,但是我不确定,我在Python上还很陌生,所以任何评论对我都非常有帮助。
You are looping over a file and loading each line as JSON. 您正在遍历文件并将每一行作为JSON加载。 Some of those JSON objects have
place
set to a dictionary, others have it set to None
. 这些JSON对象中的一些对象已将
place
设置为字典, 其他对象place
其设置为None
。
Whenever you just loaded a line with the value associated with the place
key set to None
, you'll get the AttributeError
exception. 每当您加载的行中的
place
键设置为None
,都会得到AttributeError
异常。
You need to test for this first: 您需要先对此进行测试:
place = data['place']
if place is not None:
print place['place_type']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.