How to extract text, line by line from a txt file in python

python, python-2.7

Solution

data = inp.read().splitlines()

You could do

data = inp.readlines()

or

data = list(inp)

but the latter two will leave newline characters on each line, which tends to be undesirable.

Note that since you care about order, putting your strings into any sort of `set` is not advisable - that destroys order.

Problem

I have a txt file like this : ``` audi lamborghini ferrari pagani ``` when I use this code : ``` with open("test.txt") as inp: data = set(inp.read().split()) ``` this gives data as : `['pagani', 'lamborghini', 'ferrari', 'audi']` What I want, is to extract text from the txt file, line by line such the output data is `['audi lamborghini','ferrari','pagani']` How this can be done ?

Original source