Read file into list and strip newlines
up vote
0
down vote
favorite
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 Stimpakn53 Mentatsn87 Buffoutn109 Rad-Xn125 Boozen260 Jet Antidoten311 Roentgen Rumn424 Monument Chunkn480 Bonus +1 Agilityn525 Hypo n48 RadAwayn71 Fruitn103 Iguana-on-a-stickn110 Psychon144 Super Stimpakn273 Healing Powdern334 Poisonn469 Rot Gutn481 Bonus +1 Intelligence n49 Antidoten81 Iguana-on-a-stickn106 Nuka-Colan124 Beern259 Jetn310 Gamma Gulp Beern378 Cookien473 Mutated Toen482 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'
python list strip
add a comment |
up vote
0
down vote
favorite
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 Stimpakn53 Mentatsn87 Buffoutn109 Rad-Xn125 Boozen260 Jet Antidoten311 Roentgen Rumn424 Monument Chunkn480 Bonus +1 Agilityn525 Hypo n48 RadAwayn71 Fruitn103 Iguana-on-a-stickn110 Psychon144 Super Stimpakn273 Healing Powdern334 Poisonn469 Rot Gutn481 Bonus +1 Intelligence n49 Antidoten81 Iguana-on-a-stickn106 Nuka-Colan124 Beern259 Jetn310 Gamma Gulp Beern378 Cookien473 Mutated Toen482 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'
python list strip
The code in your question is not the code generating the results shown.
– martineau
Nov 11 at 17:45
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 Stimpakn53 Mentatsn87 Buffoutn109 Rad-Xn125 Boozen260 Jet Antidoten311 Roentgen Rumn424 Monument Chunkn480 Bonus +1 Agilityn525 Hypo n48 RadAwayn71 Fruitn103 Iguana-on-a-stickn110 Psychon144 Super Stimpakn273 Healing Powdern334 Poisonn469 Rot Gutn481 Bonus +1 Intelligence n49 Antidoten81 Iguana-on-a-stickn106 Nuka-Colan124 Beern259 Jetn310 Gamma Gulp Beern378 Cookien473 Mutated Toen482 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'
python list strip
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 Stimpakn53 Mentatsn87 Buffoutn109 Rad-Xn125 Boozen260 Jet Antidoten311 Roentgen Rumn424 Monument Chunkn480 Bonus +1 Agilityn525 Hypo n48 RadAwayn71 Fruitn103 Iguana-on-a-stickn110 Psychon144 Super Stimpakn273 Healing Powdern334 Poisonn469 Rot Gutn481 Bonus +1 Intelligence n49 Antidoten81 Iguana-on-a-stickn106 Nuka-Colan124 Beern259 Jetn310 Gamma Gulp Beern378 Cookien473 Mutated Toen482 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'
python list strip
python list strip
edited Nov 11 at 17:43
martineau
65.1k987176
65.1k987176
asked Sep 28 '13 at 2:30
user2806298
47239
47239
The code in your question is not the code generating the results shown.
– martineau
Nov 11 at 17:45
add a comment |
The code in your question is not the code generating the results shown.
– martineau
Nov 11 at 17:45
The code in your question is not the code generating the results shown.
– martineau
Nov 11 at 17:45
The code in your question is not the code generating the results shown.
– martineau
Nov 11 at 17:45
add a comment |
2 Answers
2
active
oldest
votes
up vote
10
down vote
accepted
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.
... for line if temp_file ... should be ... for line in temp_file
– gwideman
Oct 27 '13 at 1:21
@gwideman: thanks; fixed.
– 9000
Oct 27 '13 at 8:00
2
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 1 at 19:54
1
@Matt-Mac-Muffin: Indeed! The explicitncuts off only the newline character and leaves any trailing spaces; stripping all whitespace is the right thing to do in most cases.
– 9000
Feb 1 at 21:04
add a comment |
up vote
0
down vote
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
Usingmap()isn't really necessary. You can use a generator expression and do it like thisfor line in (line.rstrip('n') for line in f):.
– martineau
Nov 11 at 18:00
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
accepted
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.
... for line if temp_file ... should be ... for line in temp_file
– gwideman
Oct 27 '13 at 1:21
@gwideman: thanks; fixed.
– 9000
Oct 27 '13 at 8:00
2
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 1 at 19:54
1
@Matt-Mac-Muffin: Indeed! The explicitncuts off only the newline character and leaves any trailing spaces; stripping all whitespace is the right thing to do in most cases.
– 9000
Feb 1 at 21:04
add a comment |
up vote
10
down vote
accepted
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.
... for line if temp_file ... should be ... for line in temp_file
– gwideman
Oct 27 '13 at 1:21
@gwideman: thanks; fixed.
– 9000
Oct 27 '13 at 8:00
2
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 1 at 19:54
1
@Matt-Mac-Muffin: Indeed! The explicitncuts off only the newline character and leaves any trailing spaces; stripping all whitespace is the right thing to do in most cases.
– 9000
Feb 1 at 21:04
add a comment |
up vote
10
down vote
accepted
up vote
10
down vote
accepted
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.
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.
edited Oct 27 '13 at 8:00
answered Sep 28 '13 at 2:36
9000
29k74582
29k74582
... for line if temp_file ... should be ... for line in temp_file
– gwideman
Oct 27 '13 at 1:21
@gwideman: thanks; fixed.
– 9000
Oct 27 '13 at 8:00
2
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 1 at 19:54
1
@Matt-Mac-Muffin: Indeed! The explicitncuts off only the newline character and leaves any trailing spaces; stripping all whitespace is the right thing to do in most cases.
– 9000
Feb 1 at 21:04
add a comment |
... for line if temp_file ... should be ... for line in temp_file
– gwideman
Oct 27 '13 at 1:21
@gwideman: thanks; fixed.
– 9000
Oct 27 '13 at 8:00
2
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 1 at 19:54
1
@Matt-Mac-Muffin: Indeed! The explicitncuts off only the newline character and leaves any trailing spaces; stripping all whitespace is the right thing to do in most cases.
– 9000
Feb 1 at 21:04
... for line if temp_file ... should be ... for line in temp_file
– gwideman
Oct 27 '13 at 1:21
... for line if temp_file ... should be ... for line in temp_file
– gwideman
Oct 27 '13 at 1:21
@gwideman: thanks; fixed.
– 9000
Oct 27 '13 at 8:00
@gwideman: thanks; fixed.
– 9000
Oct 27 '13 at 8:00
2
2
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 1 at 19:54
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 1 at 19:54
1
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 1 at 21:04
@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 1 at 21:04
add a comment |
up vote
0
down vote
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
Usingmap()isn't really necessary. You can use a generator expression and do it like thisfor line in (line.rstrip('n') for line in f):.
– martineau
Nov 11 at 18:00
add a comment |
up vote
0
down vote
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
Usingmap()isn't really necessary. You can use a generator expression and do it like thisfor line in (line.rstrip('n') for line in f):.
– martineau
Nov 11 at 18:00
add a comment |
up vote
0
down vote
up vote
0
down vote
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
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
answered Mar 10 '16 at 21:22
Chad Skeeters
1,1181315
1,1181315
Usingmap()isn't really necessary. You can use a generator expression and do it like thisfor line in (line.rstrip('n') for line in f):.
– martineau
Nov 11 at 18:00
add a comment |
Usingmap()isn't really necessary. You can use a generator expression and do it like thisfor line in (line.rstrip('n') for line in f):.
– martineau
Nov 11 at 18:00
Using
map() isn't really necessary. You can use a generator expression and do it like this for line in (line.rstrip('n') for line in f):.– martineau
Nov 11 at 18:00
Using
map() isn't really necessary. You can use a generator expression and do it like this for line in (line.rstrip('n') for line in f):.– martineau
Nov 11 at 18:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19062574%2fread-file-into-list-and-strip-newlines%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The code in your question is not the code generating the results shown.
– martineau
Nov 11 at 17:45