Search code examples
pythonpython-3.xpython-2.7text-filesread-write

How to read a sorted and set text file in Python?


I'm new to python and stuck up with the issue i'm reading a text file and removing unwanted and writing with sorted-set into new text file. then i need to read the new text file and continue to do next coding. But when i read the file its reading but skipping last 40 odd lines from the new text file.

please find my coding below

f3 = open("data/new.txt", "w")
uniqueperm = []
with open("data/read1.txt", "r") as ins:
    d = ins.readlines()
    for line in d:
    #print(line)

        if 'blab: name=' and 'blab1' in line:

            line = str(line.replace("blab: name= ", "").replace("'", "").replace("\n", ""))
            line = str(line.replace("blab1: ", "").replace("'", "").replace("\n", ""))
            #print(line)
            uniqueperm.append(line)
            #print((line))

for pop in (sorted(set(uniqueperm))):
#print(pop)
f3.write(pop + "\n")

packages = os.listdir("./data/")
inss = open("data/new.txt", 'r').read().split("\n")
print(inss)

output inss reads line by line from the new text file but skips last 40 odd lines in new text file. please some one help me on this issue. Thanks in Advance :)

#read1.txt file contains 

blab1: cool.add.WEATHER_ALL
blab1: warm:add.WEATHER_EVERYWHERE
blab: name= hello.add.COPY_HI
blab: name= hello.add.ACCESS_HELLO
blab: name= hello.add.ADD_HI
blab: name= hello.add.WRITE_HI
.
.
.
#it also include repeated data as above so i used set() option so the duplicates are removed before writing to new.txt file 

#new.txt file contains

hello.add.ACCESS_HELLO
hello.add.ADD_HI
hello.add.COPY_HI
hello.add.WRITE_HI
cool.add.WEATHER_ALL
warm.add.WEATHER_EVERYWHERE
.
.
.

#new.txt file contains all the data from read1.txt

#when i read this new.txt file in terminal the output is

hello.add.ACCESS_HELLO
hello.add.ADD_HI
hello.add.COPY_HI
hello.add.WRITE_HI
cool.add.WEATHER_ALL

#remaining its skipping or not reading to further proceed

Solution

  • Your condition for checking substrings is incorrect:

    if 'blab: name=' and 'blab1' in line:
    

    will check 'blab: name=' and 'blab1' in line separately (and both parts of the condition needs to hold true). What you want instead is:

    if 'blab: name=' in line or 'blab1' in line: