On 4/23/2010 10:20 AM, Red wrote:
My apologies for what is probably a simple question to most on this
group. However, I am new to python and very new to json.
I am trying to read in a json file from a twitter download. There are,
generally, two types of lines: those lines with "text" and the other
lines. I am only interested in the lines with "text". I am also only
interested in lines with "lang":"en", but I haven't gotten far enough
to implement that condition in the code snippets below.
I have gotten Option 2 below to sort of work. It works fine for
'text', but doesn't work for 'lang'.
You do not define 'work', 'sort of work', and "doesn't work".
FWIW I am using Python 2.6.4
Can someone tell me what I'm doing wrong with the following code
snippets and/or point me toward an example of an implementation?
Many thanks for your patience.
---------------------------------
import sys
import json
f = open(sys.argv[1])
#option 1
for line in f:
j = json.loads(line)
try:
'text' in j
This does not raise an exception when false
print "TEXT: ", j
so this should always print.
Forget this option.
except:
print "EXCEPTION: ", j
continue
else:
text=j['text']
----snip --------
#option 2 does basically the same thing as option 1 ,
Not at all when 'text' in not in j.
but also looks
for 'lang'
for line in f:
j = json.loads(line)
if 'text' in j:
if 'lang' in j:
lang = j['lang']
print "language", lang
text = j['text']
----snip --------
tjr
--
http://mail.python.org/mailman/listinfo/python-list