Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. t = """
  2. 'charmander',12
  3.  
  4. 'bulbasaur',7
  5.  
  6. 'squirtle',13"""
  7.  
  8. with open("data.txt","w") as f:
  9. f.write(t)
  10.  
  11. pokemons = []
  12.  
  13. with open("data.txt") as f:
  14. for line in f:
  15. line = line.strip() # remove n and other whitespaces
  16. if line: # only process non empty lines
  17. try:
  18. poke, lvl = line.split(",")
  19. except ValueError:
  20. print("Unable to split line into 2 parts: ", line)
  21. continue
  22. poke=poke.strip("'")
  23. pokemons.append( [poke,int(lvl)] )
  24.  
  25. if len(pokemons) == 6:
  26. break
  27.  
  28. print(pokemons)
  29.  
  30. [['charmander', 12], ['bulbasaur', 7], ['squirtle', 13]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement