furas

Python - read lines - learnpython.org

Nov 12th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. results = []
  2. with open('C:/Python2712/Server/list.txt') as list_data:
  3.     for line in list_data:
  4.         results.append(line.strip("\n").split(','))
  5. print results
  6.  
  7. # if you don't neeed `split(',')`
  8.  
  9. with open('C:/Python2712/Server/list.txt') as list_data:
  10.     results = list_data.read().split("\n")
  11. print results
  12.  
  13. # removing empty lines
  14.  
  15. results = []
  16. with open('C:/Python2712/Server/list.txt') as list_data:
  17.     for line in list_data:
  18.         line = line.strip() # remove "\n" (and spaces)
  19.         if line: # skip empty lines
  20.             results.append(line)
  21. print results
Add Comment
Please, Sign In to add comment