Read file into list and strip newlines











up vote
0
down vote

favorite
2












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'









share|improve this question
























  • The code in your question is not the code generating the results shown.
    – martineau
    Nov 11 at 17:45

















up vote
0
down vote

favorite
2












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'









share|improve this question
























  • The code in your question is not the code generating the results shown.
    – martineau
    Nov 11 at 17:45















up vote
0
down vote

favorite
2









up vote
0
down vote

favorite
2






2





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'









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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.






share|improve this answer























  • ... 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 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


















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





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer























  • ... 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 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















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.






share|improve this answer























  • ... 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 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 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


















  • ... 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 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
















... 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












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





share|improve this answer





















  • 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















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





share|improve this answer





















  • 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













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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 10 '16 at 21:22









Chad Skeeters

1,1181315




1,1181315












  • 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly