What is the best way to iterate over a python list, excluding certain values and printing out the result -
i new python , have question:
have checked similar questions, checked tutorial dive python, checked python documentation, googlebinging, similar stack overflow questions , dozen other tutorials.
have section of python code reads text file containing 20 tweets. able extract these 20 tweets using following code:
with open ('output.txt') fp: line in iter(fp.readline,''): tweets=json.loads(line) data.append(tweets.get('text')) i=0 while < len(data): print data[i] i=i+1
the above while loop iterates , prints out 20 tweets (lines) output.txt
. however, these 20 lines contain non-english character data "los ladillo los dos, soy maaaala o maloooooooooooo"
, urls "http://t.co/57ldpk"
, string "none"
, photos url "photo: http://t.co/kxpaaaaa
(i have edited privacy)
i purge output of (which list
), , exclude following:
- the
none
entries - anything beginning string
"photo:"
- it bonus if can exclude non-unicode data
i have tried following bits of code
- using
data.remove("none:")
errorlist.remove(x): x not in list.
- reading items not want set , doing comparison on output no luck.
- researching list comprehensions, wonder if looking @ right solution here.
i oracle background there functions chop out wanted/unwanted section of output, gone round in circles in last 2 hours on this. appreciated!
try this:
def legit(string): if (string.startswith("photo:") or "none" in string): return false else: return true whatyouwant = [x x in data if legit(x)]
i'm not sure if work out of box data, idea. if you're not familiar, [x x in data if legit(x)]
called list comprehension
Comments
Post a Comment