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")
if repeat not in line and not any(i.isdigit() for i in line):
... for file 4if line.isnumeric():
... – toing_toingfor line in file1
. To get the desired output try the following conditions:if repeat in line
,elif line.isnumeric()
,else
. – SUTerliakov900*
to be in any of result files (it's not a number, but contains digits) – SUTerliakovstrip()
, beforeisnumeric()
, and I agree with @СтаниславТерляков, that approach is more cleaner – toing_toing