Reputation: 151
I have a long file which is structured like below:
500
500
(Line 2, Line 4, basically every even line) What I want to do:
500
should be merged with not even line above (so Line 2 with Line 1, Line 4 with Line 3, etc). <Line 1>;<Line 2>
). How I can easily do it in python (2.7)?
Regex in Notepad++ may also work. I've heard that I should use a multi-line mode but I don't know how to do it. Probably it's not even hard but I have a hard time dealing with it.
Thanks.
Here the input (simplified):
;500;616;;”YPO_INV”;”KP”;”51D0072”;”YNU”
;” 6,291.00”;;
;500;6900;;”YPNV”;”KE”;”53213072”;”YOU”
;” 6,991.00”;;
Expected output:
;500;616;;”YPO_INV”;”KP”;”51D0072”;”YNU”;;” 6,291.00”;;
;500;6900;;”YPNV”;”KE”;”53213072”;”YOU”;;” 6,991.00”;;
Upvotes: 0
Views: 98
Reputation: 3405
Regex: \n^(?!;500)
Details:
^
Asserts position at start of a line(?!)
Negative LookaheadPython code:
text = open(r'C:\....txt').read()
r = re.compile(r'\n^(?!;500)', re.M)
text = r.sub(';', text)
Output:
;500;616;;”YPO_INV”;”KP”;”51D0072”;”YNU”;;” 6,291.00”;;
;500;6900;;”YPNV”;”KE”;”53213072”;”YOU”;;” 6,991.00”;;
Upvotes: 0
Reputation: 12930
Use Notepad++
for this kind of simple changes.
Enter following values:
[\r\n]+(;(?!500))
\1
Click on Replace All (Alt+A)
Upvotes: 0
Reputation: 10360
Try this regex:
[\r\n]+(?!;500)
Replace each match with a ;
Explanation:
[\r\n]+
- matches 1+ occurrences of either a newline character or a carriage return(?!;500)
- negative lookahead to make sure that the current position is not followed by :500
Before Replacing:
After replacing:
Upvotes: 1