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:

  1. the none entries
  2. anything beginning string "photo:"
  3. it bonus if can exclude non-unicode data

i have tried following bits of code

  1. using data.remove("none:") error list.remove(x): x not in list.
  2. reading items not want set , doing comparison on output no luck.
  3. 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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -