Python 3.7: How can I read a whole file with readlines() except of the first line?

python readlines
python read file line by line into list
python read file line by line example
python read file and split lines
start reading file from specific line python
python file read
write a python program to read a file line by line store it into a variable
python read string line by line

I am coding a vocabulary program where you can enter as many words as you want with their translation in another language. These words are saved in a .txt file. Then you can open this file inside the Python Console and the program will ask you a word and you should enter the translation in the other language. But in the first line I have the two languages and later I split them and I use them again. But when the program asks the vocabulary I use readlines() but then the program also asks you the translation of the language (first Line) for example:

German
Translation: 

but I don't want this, I want that the program reads every line in this file except of the first Line. And I don't know the amount of lines in this file, because the user enters as many word as he wants.

Thank you very much for your help! And here is my code where I read the lines:

with open(name + ".txt", "r") as file:
        for line in file.readlines():
            word_1, word_2 = line.split(" - ")
            newLanguage_1.append(word_1)
            newLanguage_2.append(word_2)

You could skip the first line by calling next on the fd(since file object is an iterator) like,

with open("{}.txt".format(name), "r") as file:
        next(file) # skip the first line in the file
        for line in file:
            word_1, _ , word_2 = line.strip().partition(" - ") # use str.partition for better string split
            newLanguage_1.append(word_1)
            newLanguage_2.append(word_2)

Opening files and reading from files, How do I read the first line of a file in Python? The readlines() method returns a list containing each line in the file as a list item. Use the hint parameter to limit the number of lines returned. If the total number of bytes returned exceeds the specified number, no more lines are returned.

Just skip the first line, the file object file is alraedy an iterator yielding the lines:

with open(f"{name}.txt", "r") as file:
     next(file)
     for line in file:
         word_1, word_2 = line.split(" - ")
         newLanguage_1.append(word_1)
         newLanguage_2.append(word_2)

As a comprehension:

with open(f"{name}.txt", "r") as file:
     next(file)
     newLanguage_1, newLanguage_2 = zip(*(l.split(" - ") for l in file))

How to read a text file into a string in Python, How do you skip the first line when reading a file in Python? Following is the syntax for readlines() method − fileObject.readlines( sizehint ); Parameters. sizehint − This is the number of bytes to be read from the file. Return Value. This method returns a list containing the lines. Example. The following example shows the usage of readlines() method.

You can add a counter.

with open(name + ".txt", "r") as file:
    i=0
    for line in file.readlines():
        if i==0:
            pass
        else:
            word_1, word_2 = line.split(" - ")
            newLanguage_1.append(word_1)
            newLanguage_2.append(word_2)
        i+=1

Read a File Line-by-Line in Python, What is the proper way to open a file that you intend to read from? Here is an example of how to use Python’s readlines. We first open the file using open() function in read only mode. And use the file handler from opening the file to read all lines using readlines() as follows. # Open the file with read only permit f = open('my_text_file.txt', "r") # use readlines to read all lines in the file # The variable "lines" is a list containing all lines in the file lines = f.readlines() # close the file after reading the lines. f.close() We can also read all the

5 Different ways to read a file line by line in Python – thispointer.com, What method is best for reading the entire file into a single string in Python? Open an existing file for appending plain text. rb. Open for reading binary data. wb. Open for writing binary data. Once you have written or read all of the desired data for a file object you need to close the file so that resources can be reallocated on the operating system that the code is running on. fp.close ()

Learning Python Networking: A complete guide to build and deploy , In this tutorial, we'll be reading a big file line by line in Python with the read, readline so you can pull in and process another chunk until the whole massive file has the first being a positional string parameter representing the path to the file that filepath = 'Iliad.txt' with open(filepath) as fp: line = fp.readline() cnt = 1 while  If the file is of reasonable size, another approach that uses list-comprehensionto read the entire file into a list of 2-tuples, is this: filaname = '/path/to/file/name'with open(filename, 'r') as f: list_of_2tuples = [ (line,f.readline()) for line in f ]for (line1,line2) in list_of_2tuples: # Work with them in pairs.

Python File readlines() Method, Solution for Small Files : Get list of all lines in file using readlines(). First basic and inefficient solution is using function readlines(). If we have a small file, then we can call readlines() on the file handler, it reads the whole file All the lines in list except the last one, will contain new line character in end. A Python program can read a text file using the built-in open() function. For example, below is a Python 3 program that opens lorem.txt for reading in text mode, reads the contents into a string variable named contents, closes the file, and then prints the data.

Comments
  • with open('..') as f:..next(f); for line in f:..
  • You can skip the first line from the fdusing next(fd)
  • Could you do an example with formating. I don't understand very well how you mean this
  • Yep, also when you need to split some str, use str.partition instead of str.split, so you can always expect 3 items instead of some unknown number of items :)
  • You mean I can have a definite number of items?
  • No, you will always get 3 items with str.partition, so you can do like word1, _, word2 = 'foo bar'.partition(' '), and you will get exactly foo and bar in word, and word2 respectively
  • And I just skip the first line with next() right? And it reads later every other line, no matter how many lines there are?
  • Yes, the for loop exhausts the fd as it read line by line until EOF
  • That could also be a solution but I think the next() method is easier! Thank you anyways :)