Part 2 Of A Successful Outcome Regarding White-space Filling
So, my first question was answered correctly. For reference you can go here... How to fill the white-space with info while leaving the rest unchanged? In short, I needed this... PO
Solution 1:
As pointed out in the comments, keep a reference to the previous line:
with open('in.txt') as fin, open('out.txt', 'w') as fout:
prev = None
for i, line in enumerate(fin):
if line.strip() != 'END_POLY' and prev:
fout.write(prev)
prev = line
if not i % 10000:
print('Processing line {}'.format(i))
fout.write(line)
Solution 2:
Although not in python, these types of editing is quite straighforward if you use sed
sed 'N;s/.*\n\(END_POLY\)/\1/' file.txt
Basically what it does is that it uses N
to read 2 lines at a time, if the second line contains the string END_POLY
, it removes the first line, leaving only END_POLY
Solution 3:
if you don't want duplicated data, you can trasform the list into set, then into list (taking the @Jean-François Fabre code from the other question an little modific):
import itertools, collections
with open("file.txt") as f, open("fileout.txt","w") as fw:
fw.writelines(itertools.chain.from_iterable([["BEGIN_POLYGON\n"]+list(collections.OrderedDict.fromkeys(v).keys())+["END_POLYGON\n"] for k,v in itertools.groupby(f,key = lambda l : bool(l.strip())) if k]))
as you can see, if you do:
print(list(collections.OrderedDict.fromkeys([1,1,1,1,1,1,2,2,2,2,5,3,3,3,3,3]).keys()))
it will be -> [1, 2, 5, 3]
and yo preserve the order
Post a Comment for "Part 2 Of A Successful Outcome Regarding White-space Filling"