I want to remove the new-line character of the previouse 3 lines if a line starts with a comma. I read my lines from a txt file. I am able to find the lines i want to strip the \n from, but unfortunaly my code strips all the new lines ... I would be glad for some help what is wrong with my code.
with open(in_file9, 'r', encoding='utf-8') as fin, open(out_file10, 'w', encoding='utf-8') as fout:
lines = fin.read().splitlines()
count = 0
for i, line in enumerate(lines):
count = count + 1
if line.startswith(','):
print (lines[i - 1])
lines[i - 1].strip('\n')
lines[i - 2].strip('\n')
lines[i - 3].strip('\n')
fout.write(line)
EDIT: Here is an example with a picture, because it looks not the right way when wrting it here but the pictures shows it.
*In *
6.30.230. STLB-Bau: 11/2017 068 Some text, symmety, DIN FFF 1234 (VDE
1.850 ,000 m
6.30.220. STLB-Bau: 10/2015 123 Some other text, symmetry, DIN FFF 6789 (VDE 950,000 m
Out (what i have)
6.30.230. STLB-Bau: 11/2017 068 Some text, symmety, DIN FFF 1234 (VDE 1.850 ,000 m 6.30.220. STLB-Bau: 10/2015 123 Some other text, symmetry, DIN FFF 6789 (VDE 950,000 m
Out (what i want):
6.30.230. STLB-Bau: 11/2017 068 Some text, symmety, DIN FFF 1234 (VDE 1.850,000 m
6.30.220. STLB-Bau: 10/2015 123 Some other text, symmetry, DIN FFF 6789 (VDE 950,000 m
The newlines are actually being removed by splitlines()
, since you're not providing the keepends
argument. But instead of doing fin.read().splitlines(keepends=True)
, you can just do fin.readlines()
.
The next part of the problem is that you're writing the lines to the file as you're checking them for commas, so by the time you see the comma you're looking for, it's too late to change the previous lines. Instead, modify all the lines first, then write everything to the file.
Finally, I'll note that lines[i - 1].strip('\n')
doesn't actually modify lines[i - 1]
. It simply returns a modified version of the line, but you still need to assign it back.
Addressing these issues in your code, this appears to do what you're asking for:
with open(in_file9, 'r', encoding='utf-8') as fin, open(out_file10, 'w', encoding='utf-8') as fout:
lines = fin.readlines()
for i, line in enumerate(lines):
if line.startswith(','):
lines[i - 1] = lines[i - 1].strip('\n')
lines[i - 2] = lines[i - 2].strip('\n')
lines[i - 3] = lines[i - 3].strip('\n')
fout.write(''.join(lines))
All that said, if your problem can be generalized to "if a line starts with a comma, connect it to the last non-blank line", here's a much shorter and more flexible solution using regex:
from re import sub, MULTILINE
with open(in_file9, 'r', encoding='utf-8') as fin, open(out_file10, 'w', encoding='utf-8') as fout:
lines = fin.read()
lines = sub(r'\s+^(?=,)', '', lines, flags=MULTILINE)
fout.write(lines)