0
votes

I have the following stings in a text file. I saved the file as file1.txt

What is the main reason for leaving?

Happy Easter Holidays
All the men here are just not understanding the situation
Happy Easter Holidays
In what ways can I help you
Happy Easter Holidays
You better learn how to be polite
Happy Easter Holidays
OMG that food is looking really great
Happy Easter Holidays
Well, let us try to thing about that in another way
21
40
50
100
20
100
800
900

I want to split the strings into 3 different files (file2, file3 and file4)

file2 will contain only repeated phrases in the string

file3 will contain non-repeated strings but no integer/numbers

file4 will contain only integers/numbers.

I have written the code below. the code works OK for file2, but not for file3 and file4 I need help with how to write the code to work for file3 and file4

file1 = open("file1.txt", "rt")
file2 = open("file2.txt", "wt")
file3 = open("file3.txt", "wt")
file4 = open("file4.txt", "wt")

content = file1.readlines()
repeat = "Happy Easter Holidays"
print("Processing inputs")

for line in content:
    if repeat in line:
        file2.write(line)
    if repeat not in line:
        file3.write(line)
    if line.isdigit():
        file4.write(line)

file2.close()
file3.close()
file4.close()

print("Output complete")
1
for file 3: if repeat not in line and not any(i.isdigit() for i in line): ... for file 4 if line.isnumeric(): ...toing_toing
First of all, close file1 too or better use context manager. Don't use readlines() in such cases: file descriptor itself is iterator and you can just write for line in file1. To get the desired output try the following conditions: if repeat in line, elif line.isnumeric(), else.SUTerliakov
Or the approach by @toing_noing, if you don't want line 900* to be in any of result files (it's not a number, but contains digits)SUTerliakov
@toing_toing. Thanks for the quick response. It now works for file3, but for file4, the only output i get is 900. I want all the numbers/integers (total of 8 values) to sent to file4Vincent Oke
you probably have empty spaces, try using strip(), before isnumeric(), and I agree with @СтаниславТерляков, that approach is more cleanertoing_toing

1 Answers

0
votes

While reading the content of the file python adds the newline character at the end every line and hence isnumeric() does not work

file1 = open("file1.txt", "rt")
file2 = open("file2.txt", "wt")
file3 = open("file3.txt", "wt")
file4 = open("file4.txt", "wt")

def remove_newlines(fname):
    flist = open(fname).readlines()
    return [s.rstrip('\n') for s in flist]

content=remove_newlines("file1.txt")
repeat = "Happy Easter Holidays"
print("Processing inputs")

for line in content:
    if repeat in line:
        file2.write(line+"\n")
    elif line.isnumeric():
        file4.write(line+"\n")
    else:
        file3.write(line+"\n")

file2.close()
file3.close()
file4.close()

print("Output complete")

Here i have added a function that removes newline character while reading the content