Advertisement
Guest User

Untitled

a guest
May 5th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. {"setup": "test", "punchline": "ok", "numOfRatings": 0, "sumOfRatings": 0},
  2. {"setup": "test2", "punchline": "ok2", "numOfRatings": 0, "sumOfRatings": 0}
  3.  
  4. {"setup": "test", "punchline": "ok", "numOfRatings": 0, "sumOfRatings": 0}
  5. {"setup": "test2", "punchline": "ok2", "numOfRatings": 0, "sumOfRatings": 0}
  6.  
  7. def dicts_from_file(file):
  8. dicts_from_file = []
  9. with open(file,'r') as inf:
  10. for line in inf:
  11. dicts_from_file.append(eval(line))
  12. return dicts_from_file
  13.  
  14. def get_setups(dicts):
  15. setups = []
  16. for dict in dicts:
  17. for key in dict:
  18. if key == "setup":
  19. setups.append(dict[key])
  20. return setups
  21.  
  22. print get_setups(dicts_from_file("data.txt"))
  23.  
  24. f = open('data')
  25. for line in f:
  26. d = ast.literal_eval(line)[0]
  27. print d['setup']
  28.  
  29. f = open('data')
  30. for line in f:
  31. d = ast.literal_eval(line)
  32. print d['setup']
  33.  
  34. def get_setup_from_file(file_name):
  35. result = []
  36. f = open(file_name, "r")
  37. for line in f.xreadlines():
  38. # or line_dict = json.loads(line)
  39. line_dict = eval(line) # if line end witch ',', try eval(line[0:-1])
  40. result.append(line_dict["setup"])
  41. return result
  42.  
  43. with open(file,'r') as file_input:
  44. for line in file_input:
  45. print eval(line).get("setup")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement