I work in Python 3.8.0, I wanted to remove strings that began with a lowercase letter it does work only those strings are empty and those empty strings I can't remove. I have tried to strip it, and it works, but not for the empty strings. My code below
gezegdes_file = open(gezegdes_betekenis, 'r')
content = gezegdes_file.readlines()
gezegdes_file.close()
gezegdes_opgeschoond = open('gezegdes_opgeschoond.txt', 'w')
for rule in content:
rule = rule.replace(rule.lower(), ' ').strip()
# rule = "".join([s for s in rule.strip().splitlines(True) if s.strip("\r\n")])
rule = rule.replace('ij', 'y')
# rule = rule.replace('\n\n','\n').replace('\n\n','\n')
rule = rule.replace('.', '').replace('!', '').replace('?', '').upper()
print(rule)
gezegdes_opgeschoond.write(str(rule) + '\n')
gezegdes_opgeschoond.close()
I have tried to replace rule.lower by empty string and it works, but I can't remove the empty strings. Result of code below
AAN DE RAND VAN HET RAVYN BLOEIEN DE MOOISTE BLOEMEN
AAN DE VEREN KENT MEN DE VOGEL
AAN DE VRUCHTEN KENT MEN DE BOOM
AAP WAT HEB JE MOOIE JONGEN
ACHTER DE WOLKEN SCHYNT DE ZON
AL DOENDE LEERT MEN
AL IS DE LEUGEN NOG ZO SNEL, DE WAARHEID ACHTERHAALT HAAR WEL
ALLE GEKHEID OP EEN STOKJE
ALLE HENS AAN DEK
ALLE HOUT IS GEEN TIMMERHOUT
ALLE WEGEN LEIDEN NAAR ROME
ALS APEN HOGER KLIMMEN WILLEN, ZIET MEN GAUW HUN BLOTE BILLEN
And there is a white line between the two last sentences, and there was a string the began with a lowercase letter between the two last lines, and all strings that began with lowercase letter have I to remove. The file is quite long and there are several empty lines in the file like this because I have to removed the strings that began with a lowercase letter. What could be the solution to remove the white line between the two last sentences?
One way is to simply skip an empty line to begin with, so you could shortcut your loop like this:
gezegdes_file = open(gezegdes_betekenis, 'r')
content = gezegdes_file.readlines()
gezegdes_file.close()
gezegdes_opgeschoond = open('gezegdes_opgeschoond.txt', 'w')
for rule in content:
if not rule: # if it's an empty string, skip everything
continue
rule = rule.replace(rule.lower(), ' ').strip()
# rule = "".join([s for s in rule.strip().splitlines(True) if s.strip("\r\n")])
rule = rule.replace('ij', 'y')
# rule = rule.replace('\n\n','\n').replace('\n\n','\n')
rule = rule.replace('.', '').replace('!', '').replace('?', '').upper()
print(rule)
gezegdes_opgeschoond.write(str(rule) + '\n')
gezegdes_opgeschoond.close()
The test line can be adjusted if you have somethings slightly different than rule == ''
but this should give you an idea.
EDIT
As @khelwood makes evident, there could be a case where after all those replacements are run, we may end up with an empty line, hence it would be safer to move the if
statement check at the end (or in addition) to the replacement block. I choose to move it down. In this case, if rule
is not empty, we will print and write it.
gezegdes_file = open(gezegdes_betekenis, 'r')
content = gezegdes_file.readlines()
gezegdes_file.close()
gezegdes_opgeschoond = open('gezegdes_opgeschoond.txt', 'w')
for rule in content:
rule = rule.replace(rule.lower(), ' ').strip()
# rule = "".join([s for s in rule.strip().splitlines(True) if s.strip("\r\n")])
rule = rule.replace('ij', 'y')
# rule = rule.replace('\n\n','\n').replace('\n\n','\n')
rule = rule.replace('.', '').replace('!', '').replace('?', '').upper()
if rule: # if it's not an empty string, then print/write
print(rule)
gezegdes_opgeschoond.write(str(rule) + '\n')
gezegdes_opgeschoond.close()