I have an existing txt file(test1) like that,
line1
line2
supp-linex
line3
supp-linex
line4
line5
I want to find the line with "supp" and add this line directly behind the previous line like,(others blank line is not change)
line1
line2linex
line3linex
line4
line5
I know less about how to tackling txt file so in this code,
a_file = open("test1.txt", "r")
lines = a_file.readlines()
a_file.close()
new_file = open("test2.txt", "w")
for line in lines:
if "supp" in line:
#del blank and concat line,I dont know how to del and concat in detail
new_file.write(lines)
new_file.close()
CodePudding user response:
You can use a new list to save the result.
with open("test1.txt") as f:
a_file = f.read().splitlines()
b_file = []
for line in a_file:
if line.startswith('supp-'):
# Removes previous empty lines if possible.
while b_file and len(b_file[-1]) == 0:
b_file.pop()
if b_file:
# Concatenate previous line
b_file[-1] = line[5:]
else:
# When there's no previous lines, appends as is
b_file.append(line[5:])
else:
b_file.append(line)
with open('test2.txt', 'w') as f:
f.write('\n'.join(b_file) '\n')
CodePudding user response:
Here is a way that does it without a new list
a_file = open("test.txt", "r")
lines = a_file.readlines()
a_file.close()
new_file = open("test2.txt", "w")
for i, line in enumerate(lines):
if "supp" in line:
j = i
while lines[j-1] == "\n":
del(lines[j-1])
j -= 1
lines[j-1] = lines[j-1].strip() line.strip("supp-")
del(lines[j])
for line in lines:
new_file.write(line)
new_file.close()
CodePudding user response:
You could use re
(regular expression) module in Python's standard library to find the pattern and replace it via module's sub()
function. To help understand how it works, think of the contents of the whole text file as a single long string containing this:
"line1\nline2\n\nsupp-linex\n\nline3\n\nsupp-linex\nline4\nline5\n"
The regular expression pattern shown in the code below matches a line of characters followed by a blank line, then another prefixed with literal string "supp-"
. The groups of characters from the match group are also assigned the names prev
and extra
so they can easily be referred to in the replacement text. The substitution process is applied to the whole file with one sub()
call, and then the result of that gets written out to the second text file.
Note: There's a really good Regular Expression HOWTO in the onliinr documentation.
import re
with open('test1.txt', 'r') as a_file:
lines = a_file.read()
pattern = r'''(?P<prev>. )\n\nsupp-(?P<extra>. )\n'''
replacement = r'''\g<prev>\g<extra>\n'''
with open('test2.txt', 'w') as new_file:
result = re.sub(pattern, replacement, lines)
new_file.write(result)
print('fini')
Here's the contents of test2.txt
after running:
line1
line2linex
line3linex
line4
line5