14

I'm having issues in reading a file into a list, When I do it only creates one item from the entire file rather than reading each element into its own field. I'm using \n as the thing to strip on, but I can't get it to work correctly.

temp = open('drugs')
drugs = [temp.read().strip("\n")]
temp.close

Result:

['40 Stimpak\n53 Mentats\n87 Buffout\n109 Rad-X\n125 Booze\n260 Jet Antidote\n311 Roentgen Rum\n424 Monument Chunk\n480 Bonus +1 Agility\n525 Hypo \n48 RadAway\n71 Fruit\n103 Iguana-on-a-stick\n110 Psycho\n144 Super Stimpak\n273 Healing Powder\n334 Poison\n469 Rot Gut\n481 Bonus +1 Intelligence \n49 Antidote\n81 Iguana-on-a-stick\n106 Nuka-Cola\n124 Beer\n259 Jet\n310 Gamma Gulp Beer\n378 Cookie\n473 Mutated Toe\n482 Bonus +1 Strength ']
drugs.strip('\n')

Traceback (most recent call last):
   File "seek", line 18, in <module>
     print drugs.strip('\n')
AttributeError: 'list' object has no attribute 'strip'
martineau
  • 119,623
  • 25
  • 170
  • 301
JohnF1NK
  • 427
  • 2
  • 7
  • 14

3 Answers3

26

file.read() reads entire file's contents, unless you specify max length. What you must be meaning is .readlines(). But you can go even more idiomatic with a list comprehension:

with open('drugs') as temp_file:
  drugs = [line.rstrip('\n') for line in temp_file]

The with statement will take care of closing the file.

9000
  • 39,899
  • 9
  • 66
  • 104
  • 3
    note that the '\n' argument is not needed in most situations if you only want to strip new lines and spaces – Matt-Mac-Muffin Feb 01 '18 at 19:54
  • 1
    @Matt-Mac-Muffin: Indeed! The explicit `\n` cuts off only the newline character and leaves any trailing spaces; stripping all whitespace is the right thing to do in most cases. – 9000 Feb 01 '18 at 21:04
13

If you're okay with reading the entire file's contents into memory, you can also use str.splitlines()

with open('your_file.txt') as f:
    lines = f.read().splitlines()

splitlines() is similar to split('\n') but if your file ends with a newline, split('\n') will return an empty string at the very end, whereas splitlines() handles this case the way you want.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
-1

This incorporates the strip directly into the for statement.

with open('drugs', 'r') as f:
  for line in map(lambda line: line.rstrip('\n'), f):
    print line

Or, if you know you don't need any space before or after text on a line, you can use this.

import string

with open('drugs', 'r') as f:
  for line in map(string.strip, f):
    print line
Chad Skeeters
  • 1,468
  • 16
  • 19
  • 2
    Using `map()` isn't really necessary. You can use a [generator expression](https://docs.python.org/3/reference/expressions.html#generator-expressions) and do it like this `for line in (line.rstrip('\n') for line in f):`. – martineau Nov 11 '18 at 18:00
  • The function `string.strip()` is available only in Python versions before 3 and it was deprecated since Python 2.4 (2004) already. – pabouk - Ukraine stay strong May 02 '20 at 10:20