First line fails to load from text file in Python

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


while line:
    for x in line:



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

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.

Opening files and reading from files, The basic pattern of opening and reading files in Python; How to open a file – an myfile = open("example.txt") for line in myfile: print(line) myfile.close() Python interpreter, try to open a filename that you know does not exist on your Desktop, and We haven't covered how to write to a file (which is a far more dangerous  In the result, we firstly review the current content of the test2.txt file using the “read_file” program, then we use this new program to add an extra line “See you tomorrow!” and review the content of the “test2.txt” again.

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:

Reading data from file, The task now is to understand how Python programs can read and write files. goal of our first example of reading files is to read the measurement values in data.txt To this end, load the file into a text editor or viewer (one can use emacs , vim The basic technique for reading the file line by line applies a for loop like this:. @Evpok "In Python 3 if the file is ascii or utf8 you don't have to specify the file encoding" - if only that were strictly true! The reality is slightly more messy; as noted in the docs, the default encoding used is platform-dependent (and may differ even on the same computer depending upon how you launch Python - for instance, I have seen code that worked at my normal shell by assuming UTF-8

To answer your additional question how to lowercase all characters:


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.

Read a File Line-by-Line in Python, In this tutorial, we'll be reading a big file line by line in Python with the read, readline and is the need to simply open a file, parse its contents, and do something with it. the first being a positional string parameter representing the path to the file that sys.argv[1] if not os.path.isfile(filepath): print("File path {} does not exist. To read only the first line of a file, open the file in read mode and call readline method on the file object. For example, f = open('my_file.txt', 'r') line = f.readline() print line f.close() The above code reads first line from my_file.txt and prints to stdout.

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:

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()]


[['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', '.']]

[PDF] File input and output and conditionals, Python will read, write or append to a file according to the access import sys filename = sys.argv[1]. myFile = open(filename, "r"). firstLine = myFile.readline(). So, in this article we have explored ways to read a text file line-by-line in two ways, including a way that I feel is a bit more Pythonic (this being the second way demonstrated in To wrap things up I presented a trivial application that is potentially useful for reading in and preprocessing data that could be used for text

Python 2.7 Tutorial, Before proceeding, make sure you understand the concepts of file path and CWD​. is newly created where is # write to myfile myfile.close() # Closing file. First off, .read() reads in the entire text content of the file as a single string. which prints out a string on its own new line, writing methods will not tack on a​  How to Read a File line by line You can also read your .txt file line by line if your data is too big to read. readlines() code will segregate your data in easy to ready mode. When you run the code ( f1=f.readlines()) for reading the file or document line by line, it will separate each line and present the file in a readable format.

10.2. fileinput, This document is for an old version of Python that is no longer supported. import fileinput for line in fileinput.input(): process(line) All files are opened in text mode by default, but you can override this by read the first line from the next file (if any); lines not read from the file will not count towards the cumulative line count. In this lesson we're going to talk about that how to read the first line of a file in python language.

How to skip the first line of a text file when reading it?, txt');. then look at the values of double(foo). Values of 10 and/or 13 represent line breaks. Numbers should  Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line character (‘ ’) in python by default. Binary files: In this type of file, there is no terminator for a line and the data is stored after converting it into machine understandable binary language.