First line fails to load from text file in Python





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm new to Python I do not understand why my code fails to load the first line. Could someone please take a look?



My code is:



f = open("test.txt")
line = f.readline()

joined=

while line:
line=f.readline().split()
for x in line:
joined.append(line)

f.close()

print(joined)


"test.txt" file looks like this:



This is the 1st line !
This is the 2nd line .
This is the 3rd line ?
This is the 4th line
This is the 5th line .


I get this (first line is missing, also duplicate entries):



[['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.']]


But desired output is:



[['This', 'is', 'the', '1st', 'line', '!'], ['This', 'is', 'the', '2nd', 'line', '.'],  ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.']]


Also, is there a way to lowercase all characters in all of the lists?










share|improve this question

























  • Is it just me, but I cannot load the first line of the text file?!

    – Glupan123
    Nov 26 '18 at 18:52


















0















I'm new to Python I do not understand why my code fails to load the first line. Could someone please take a look?



My code is:



f = open("test.txt")
line = f.readline()

joined=

while line:
line=f.readline().split()
for x in line:
joined.append(line)

f.close()

print(joined)


"test.txt" file looks like this:



This is the 1st line !
This is the 2nd line .
This is the 3rd line ?
This is the 4th line
This is the 5th line .


I get this (first line is missing, also duplicate entries):



[['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.']]


But desired output is:



[['This', 'is', 'the', '1st', 'line', '!'], ['This', 'is', 'the', '2nd', 'line', '.'],  ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.']]


Also, is there a way to lowercase all characters in all of the lists?










share|improve this question

























  • Is it just me, but I cannot load the first line of the text file?!

    – Glupan123
    Nov 26 '18 at 18:52














0












0








0








I'm new to Python I do not understand why my code fails to load the first line. Could someone please take a look?



My code is:



f = open("test.txt")
line = f.readline()

joined=

while line:
line=f.readline().split()
for x in line:
joined.append(line)

f.close()

print(joined)


"test.txt" file looks like this:



This is the 1st line !
This is the 2nd line .
This is the 3rd line ?
This is the 4th line
This is the 5th line .


I get this (first line is missing, also duplicate entries):



[['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.']]


But desired output is:



[['This', 'is', 'the', '1st', 'line', '!'], ['This', 'is', 'the', '2nd', 'line', '.'],  ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.']]


Also, is there a way to lowercase all characters in all of the lists?










share|improve this question
















I'm new to Python I do not understand why my code fails to load the first line. Could someone please take a look?



My code is:



f = open("test.txt")
line = f.readline()

joined=

while line:
line=f.readline().split()
for x in line:
joined.append(line)

f.close()

print(joined)


"test.txt" file looks like this:



This is the 1st line !
This is the 2nd line .
This is the 3rd line ?
This is the 4th line
This is the 5th line .


I get this (first line is missing, also duplicate entries):



[['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.']]


But desired output is:



[['This', 'is', 'the', '1st', 'line', '!'], ['This', 'is', 'the', '2nd', 'line', '.'],  ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.']]


Also, is there a way to lowercase all characters in all of the lists?







python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 19:12









PGCodeRider

2,2141928




2,2141928










asked Nov 26 '18 at 18:41









Glupan123Glupan123

31




31













  • Is it just me, but I cannot load the first line of the text file?!

    – Glupan123
    Nov 26 '18 at 18:52



















  • Is it just me, but I cannot load the first line of the text file?!

    – Glupan123
    Nov 26 '18 at 18:52

















Is it just me, but I cannot load the first line of the text file?!

– Glupan123
Nov 26 '18 at 18:52





Is it just me, but I cannot load the first line of the text file?!

– Glupan123
Nov 26 '18 at 18:52












4 Answers
4






active

oldest

votes


















0














You can use a list comprehension that iterates through lines splitting on space:



with open('test.txt') as f:
print([x.split() for x in f])

# [['This', 'is', 'the', '1st', 'line', '!'],
# ['This', 'is', 'the', '2nd', 'line', '.'],
# ['This', 'is', 'the', '3rd', 'line', '?'],
# ['This', 'is', 'the', '4th', 'line'],
# ['This', 'is', 'the', '5th', 'line', '.']]


To make all lower case:



print([x.lower().split() for x in f])

# [['this', 'is', 'the', '1st', 'line', '!'],
# ['this', 'is', 'the', '2nd', 'line', '.'],
# ['this', 'is', 'the', '3rd', 'line', '?'],
# ['this', 'is', 'the', '4th', 'line'],
# ['this', 'is', 'the', '5th', 'line', '.']]




The reason why you are not getting the first line is because you do .readline() outside loop and never used it. You immediately replace that with another .readline() in the loop leaving you with lines from second position onwards.






share|improve this answer


























  • Oh wow! This works perfectly! Thank you so much. I still don't get why the first line was missing?!

    – Glupan123
    Nov 26 '18 at 18:53











  • @Glupan123, Please find the explanation.

    – Austin
    Nov 26 '18 at 18:58






  • 1





    Thank you so much! Works like a charm.

    – Glupan123
    Nov 26 '18 at 19:10



















0














You're discarding the value returned by the first readline(), which is why the first line of your file is missing in the output. You can iterate over the file object as an iterator instead:



joined = 
for line in f:
joined.append(line.split())
print(joined)





share|improve this answer


























  • Thank you for this clarification... I did not know that the first "readline()" also removes the first line in the output.

    – Glupan123
    Nov 26 '18 at 19:03



















0














To answer your additional question how to lowercase all characters:



str.lower()


gives you the lowercase of a string. So to complete the above answer:



with open('test.txt') as f:
print([x.split().lower() for x in f])


This should do it.






share|improve this answer
























  • Lists don't have .lower().

    – Austin
    Nov 26 '18 at 19:00











  • Thank you for this suggestion... but I get the following error: AttributeError: 'list' object has no attribute 'lower'

    – Glupan123
    Nov 26 '18 at 19:01





















0














There are a couple issues here. The first is that the first line is not appended to your joined list because it is defined and then overwritten at the beginning of the while loop on the first iteration before it can be appended to the joined list.



The second is that you write:



for x in line:
joined.append(line)


Which adds the whole line to joined for each element in line. So the second lines gets appended 6 times because it contains 6 elements. I think you meant to append x to joined, but that will give you all the words in a single list rather than nested lists as desired. Since each line is already split into a list of individual words, there is no need for this inner loop.



You can write your code more concisely with:



with open("test.txt", "r") as f:
joined = [line.split() for line in f.readlines()]


Result:



[['This', 'is', 'the', '1st', 'line', '!'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.']]





share|improve this answer


























  • Thank you Sir for this great clarification. Now the duplicates make sense.

    – Glupan123
    Nov 26 '18 at 18:55












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',
autoActivateHeartbeat: false,
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%2f53487168%2ffirst-line-fails-to-load-from-text-file-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can use a list comprehension that iterates through lines splitting on space:



with open('test.txt') as f:
print([x.split() for x in f])

# [['This', 'is', 'the', '1st', 'line', '!'],
# ['This', 'is', 'the', '2nd', 'line', '.'],
# ['This', 'is', 'the', '3rd', 'line', '?'],
# ['This', 'is', 'the', '4th', 'line'],
# ['This', 'is', 'the', '5th', 'line', '.']]


To make all lower case:



print([x.lower().split() for x in f])

# [['this', 'is', 'the', '1st', 'line', '!'],
# ['this', 'is', 'the', '2nd', 'line', '.'],
# ['this', 'is', 'the', '3rd', 'line', '?'],
# ['this', 'is', 'the', '4th', 'line'],
# ['this', 'is', 'the', '5th', 'line', '.']]




The reason why you are not getting the first line is because you do .readline() outside loop and never used it. You immediately replace that with another .readline() in the loop leaving you with lines from second position onwards.






share|improve this answer


























  • Oh wow! This works perfectly! Thank you so much. I still don't get why the first line was missing?!

    – Glupan123
    Nov 26 '18 at 18:53











  • @Glupan123, Please find the explanation.

    – Austin
    Nov 26 '18 at 18:58






  • 1





    Thank you so much! Works like a charm.

    – Glupan123
    Nov 26 '18 at 19:10
















0














You can use a list comprehension that iterates through lines splitting on space:



with open('test.txt') as f:
print([x.split() for x in f])

# [['This', 'is', 'the', '1st', 'line', '!'],
# ['This', 'is', 'the', '2nd', 'line', '.'],
# ['This', 'is', 'the', '3rd', 'line', '?'],
# ['This', 'is', 'the', '4th', 'line'],
# ['This', 'is', 'the', '5th', 'line', '.']]


To make all lower case:



print([x.lower().split() for x in f])

# [['this', 'is', 'the', '1st', 'line', '!'],
# ['this', 'is', 'the', '2nd', 'line', '.'],
# ['this', 'is', 'the', '3rd', 'line', '?'],
# ['this', 'is', 'the', '4th', 'line'],
# ['this', 'is', 'the', '5th', 'line', '.']]




The reason why you are not getting the first line is because you do .readline() outside loop and never used it. You immediately replace that with another .readline() in the loop leaving you with lines from second position onwards.






share|improve this answer


























  • Oh wow! This works perfectly! Thank you so much. I still don't get why the first line was missing?!

    – Glupan123
    Nov 26 '18 at 18:53











  • @Glupan123, Please find the explanation.

    – Austin
    Nov 26 '18 at 18:58






  • 1





    Thank you so much! Works like a charm.

    – Glupan123
    Nov 26 '18 at 19:10














0












0








0







You can use a list comprehension that iterates through lines splitting on space:



with open('test.txt') as f:
print([x.split() for x in f])

# [['This', 'is', 'the', '1st', 'line', '!'],
# ['This', 'is', 'the', '2nd', 'line', '.'],
# ['This', 'is', 'the', '3rd', 'line', '?'],
# ['This', 'is', 'the', '4th', 'line'],
# ['This', 'is', 'the', '5th', 'line', '.']]


To make all lower case:



print([x.lower().split() for x in f])

# [['this', 'is', 'the', '1st', 'line', '!'],
# ['this', 'is', 'the', '2nd', 'line', '.'],
# ['this', 'is', 'the', '3rd', 'line', '?'],
# ['this', 'is', 'the', '4th', 'line'],
# ['this', 'is', 'the', '5th', 'line', '.']]




The reason why you are not getting the first line is because you do .readline() outside loop and never used it. You immediately replace that with another .readline() in the loop leaving you with lines from second position onwards.






share|improve this answer















You can use a list comprehension that iterates through lines splitting on space:



with open('test.txt') as f:
print([x.split() for x in f])

# [['This', 'is', 'the', '1st', 'line', '!'],
# ['This', 'is', 'the', '2nd', 'line', '.'],
# ['This', 'is', 'the', '3rd', 'line', '?'],
# ['This', 'is', 'the', '4th', 'line'],
# ['This', 'is', 'the', '5th', 'line', '.']]


To make all lower case:



print([x.lower().split() for x in f])

# [['this', 'is', 'the', '1st', 'line', '!'],
# ['this', 'is', 'the', '2nd', 'line', '.'],
# ['this', 'is', 'the', '3rd', 'line', '?'],
# ['this', 'is', 'the', '4th', 'line'],
# ['this', 'is', 'the', '5th', 'line', '.']]




The reason why you are not getting the first line is because you do .readline() outside loop and never used it. You immediately replace that with another .readline() in the loop leaving you with lines from second position onwards.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 19:11

























answered Nov 26 '18 at 18:50









AustinAustin

13k31031




13k31031













  • Oh wow! This works perfectly! Thank you so much. I still don't get why the first line was missing?!

    – Glupan123
    Nov 26 '18 at 18:53











  • @Glupan123, Please find the explanation.

    – Austin
    Nov 26 '18 at 18:58






  • 1





    Thank you so much! Works like a charm.

    – Glupan123
    Nov 26 '18 at 19:10



















  • Oh wow! This works perfectly! Thank you so much. I still don't get why the first line was missing?!

    – Glupan123
    Nov 26 '18 at 18:53











  • @Glupan123, Please find the explanation.

    – Austin
    Nov 26 '18 at 18:58






  • 1





    Thank you so much! Works like a charm.

    – Glupan123
    Nov 26 '18 at 19:10

















Oh wow! This works perfectly! Thank you so much. I still don't get why the first line was missing?!

– Glupan123
Nov 26 '18 at 18:53





Oh wow! This works perfectly! Thank you so much. I still don't get why the first line was missing?!

– Glupan123
Nov 26 '18 at 18:53













@Glupan123, Please find the explanation.

– Austin
Nov 26 '18 at 18:58





@Glupan123, Please find the explanation.

– Austin
Nov 26 '18 at 18:58




1




1





Thank you so much! Works like a charm.

– Glupan123
Nov 26 '18 at 19:10





Thank you so much! Works like a charm.

– Glupan123
Nov 26 '18 at 19:10













0














You're discarding the value returned by the first readline(), which is why the first line of your file is missing in the output. You can iterate over the file object as an iterator instead:



joined = 
for line in f:
joined.append(line.split())
print(joined)





share|improve this answer


























  • Thank you for this clarification... I did not know that the first "readline()" also removes the first line in the output.

    – Glupan123
    Nov 26 '18 at 19:03
















0














You're discarding the value returned by the first readline(), which is why the first line of your file is missing in the output. You can iterate over the file object as an iterator instead:



joined = 
for line in f:
joined.append(line.split())
print(joined)





share|improve this answer


























  • Thank you for this clarification... I did not know that the first "readline()" also removes the first line in the output.

    – Glupan123
    Nov 26 '18 at 19:03














0












0








0







You're discarding the value returned by the first readline(), which is why the first line of your file is missing in the output. You can iterate over the file object as an iterator instead:



joined = 
for line in f:
joined.append(line.split())
print(joined)





share|improve this answer















You're discarding the value returned by the first readline(), which is why the first line of your file is missing in the output. You can iterate over the file object as an iterator instead:



joined = 
for line in f:
joined.append(line.split())
print(joined)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 18:54

























answered Nov 26 '18 at 18:48









blhsingblhsing

43k41743




43k41743













  • Thank you for this clarification... I did not know that the first "readline()" also removes the first line in the output.

    – Glupan123
    Nov 26 '18 at 19:03



















  • Thank you for this clarification... I did not know that the first "readline()" also removes the first line in the output.

    – Glupan123
    Nov 26 '18 at 19:03

















Thank you for this clarification... I did not know that the first "readline()" also removes the first line in the output.

– Glupan123
Nov 26 '18 at 19:03





Thank you for this clarification... I did not know that the first "readline()" also removes the first line in the output.

– Glupan123
Nov 26 '18 at 19:03











0














To answer your additional question how to lowercase all characters:



str.lower()


gives you the lowercase of a string. So to complete the above answer:



with open('test.txt') as f:
print([x.split().lower() for x in f])


This should do it.






share|improve this answer
























  • Lists don't have .lower().

    – Austin
    Nov 26 '18 at 19:00











  • Thank you for this suggestion... but I get the following error: AttributeError: 'list' object has no attribute 'lower'

    – Glupan123
    Nov 26 '18 at 19:01


















0














To answer your additional question how to lowercase all characters:



str.lower()


gives you the lowercase of a string. So to complete the above answer:



with open('test.txt') as f:
print([x.split().lower() for x in f])


This should do it.






share|improve this answer
























  • Lists don't have .lower().

    – Austin
    Nov 26 '18 at 19:00











  • Thank you for this suggestion... but I get the following error: AttributeError: 'list' object has no attribute 'lower'

    – Glupan123
    Nov 26 '18 at 19:01
















0












0








0







To answer your additional question how to lowercase all characters:



str.lower()


gives you the lowercase of a string. So to complete the above answer:



with open('test.txt') as f:
print([x.split().lower() for x in f])


This should do it.






share|improve this answer













To answer your additional question how to lowercase all characters:



str.lower()


gives you the lowercase of a string. So to complete the above answer:



with open('test.txt') as f:
print([x.split().lower() for x in f])


This should do it.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 18:55









SilvanSilvan

537




537













  • Lists don't have .lower().

    – Austin
    Nov 26 '18 at 19:00











  • Thank you for this suggestion... but I get the following error: AttributeError: 'list' object has no attribute 'lower'

    – Glupan123
    Nov 26 '18 at 19:01





















  • Lists don't have .lower().

    – Austin
    Nov 26 '18 at 19:00











  • Thank you for this suggestion... but I get the following error: AttributeError: 'list' object has no attribute 'lower'

    – Glupan123
    Nov 26 '18 at 19:01



















Lists don't have .lower().

– Austin
Nov 26 '18 at 19:00





Lists don't have .lower().

– Austin
Nov 26 '18 at 19:00













Thank you for this suggestion... but I get the following error: AttributeError: 'list' object has no attribute 'lower'

– Glupan123
Nov 26 '18 at 19:01







Thank you for this suggestion... but I get the following error: AttributeError: 'list' object has no attribute 'lower'

– Glupan123
Nov 26 '18 at 19:01













0














There are a couple issues here. The first is that the first line is not appended to your joined list because it is defined and then overwritten at the beginning of the while loop on the first iteration before it can be appended to the joined list.



The second is that you write:



for x in line:
joined.append(line)


Which adds the whole line to joined for each element in line. So the second lines gets appended 6 times because it contains 6 elements. I think you meant to append x to joined, but that will give you all the words in a single list rather than nested lists as desired. Since each line is already split into a list of individual words, there is no need for this inner loop.



You can write your code more concisely with:



with open("test.txt", "r") as f:
joined = [line.split() for line in f.readlines()]


Result:



[['This', 'is', 'the', '1st', 'line', '!'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.']]





share|improve this answer


























  • Thank you Sir for this great clarification. Now the duplicates make sense.

    – Glupan123
    Nov 26 '18 at 18:55
















0














There are a couple issues here. The first is that the first line is not appended to your joined list because it is defined and then overwritten at the beginning of the while loop on the first iteration before it can be appended to the joined list.



The second is that you write:



for x in line:
joined.append(line)


Which adds the whole line to joined for each element in line. So the second lines gets appended 6 times because it contains 6 elements. I think you meant to append x to joined, but that will give you all the words in a single list rather than nested lists as desired. Since each line is already split into a list of individual words, there is no need for this inner loop.



You can write your code more concisely with:



with open("test.txt", "r") as f:
joined = [line.split() for line in f.readlines()]


Result:



[['This', 'is', 'the', '1st', 'line', '!'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.']]





share|improve this answer


























  • Thank you Sir for this great clarification. Now the duplicates make sense.

    – Glupan123
    Nov 26 '18 at 18:55














0












0








0







There are a couple issues here. The first is that the first line is not appended to your joined list because it is defined and then overwritten at the beginning of the while loop on the first iteration before it can be appended to the joined list.



The second is that you write:



for x in line:
joined.append(line)


Which adds the whole line to joined for each element in line. So the second lines gets appended 6 times because it contains 6 elements. I think you meant to append x to joined, but that will give you all the words in a single list rather than nested lists as desired. Since each line is already split into a list of individual words, there is no need for this inner loop.



You can write your code more concisely with:



with open("test.txt", "r") as f:
joined = [line.split() for line in f.readlines()]


Result:



[['This', 'is', 'the', '1st', 'line', '!'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.']]





share|improve this answer















There are a couple issues here. The first is that the first line is not appended to your joined list because it is defined and then overwritten at the beginning of the while loop on the first iteration before it can be appended to the joined list.



The second is that you write:



for x in line:
joined.append(line)


Which adds the whole line to joined for each element in line. So the second lines gets appended 6 times because it contains 6 elements. I think you meant to append x to joined, but that will give you all the words in a single list rather than nested lists as desired. Since each line is already split into a list of individual words, there is no need for this inner loop.



You can write your code more concisely with:



with open("test.txt", "r") as f:
joined = [line.split() for line in f.readlines()]


Result:



[['This', 'is', 'the', '1st', 'line', '!'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.']]






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 18:58

























answered Nov 26 '18 at 18:52









Henry WoodyHenry Woody

5,04531127




5,04531127













  • Thank you Sir for this great clarification. Now the duplicates make sense.

    – Glupan123
    Nov 26 '18 at 18:55



















  • Thank you Sir for this great clarification. Now the duplicates make sense.

    – Glupan123
    Nov 26 '18 at 18:55

















Thank you Sir for this great clarification. Now the duplicates make sense.

– Glupan123
Nov 26 '18 at 18:55





Thank you Sir for this great clarification. Now the duplicates make sense.

– Glupan123
Nov 26 '18 at 18:55


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53487168%2ffirst-line-fails-to-load-from-text-file-in-python%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

Wiesbaden

Marschland

Dieringhausen