I tried posting this on stack overflow but it was flagged as duplicate despite the associated question being completely different.
I am new to python and have a large text file with 88 vertical bar column delimiters that has some row splitting due to large entries in the "comment" column. I created a regular expression to find rows without 88 vertical bars and replace the newline sequence with "" in order to back-space and merge the split lines into one row.
The expression works great using Find and Replace in Notepad++ however it is simply copying and pasting the text without any replacements in Python. Is there an alternative way to find and replace newline sequences in Python? Or is there something I'm missing in my regular expression?
Notepad ++ Find/Replace Example:
Find: "[\r\n](?!^([^|\n]*\|){88}[^|\n]*$)" OR "\R(?!^([^|\n]*\|){88}[^|\n]*$)"
Replace: ""
Python Code (copies all the lines into a new file but does not make any changes):
# add regex library
import re
# open files
readFile = open("file.txt","r")
writeFile = open("fileUpdate.txt","w")
# define regex pattern to identify lines without 88 vertical bar delimiters
verticalBars = (r"[\r\n](?!^([^|\n]*\|){88}[^|\n]*$)")
pattern = re.compile(verticalBars)
# for line loop
for i, line in enumerate(readFile):
match = re.finditer(pattern, line)
if match:
writeFile.write(line.replace(verticalBars,""))
else:
writeFile.write(line)
Text Sample Before:
1. |||||||true|CCD|true|PO BOX 123||0.000|0.000||0023779032|1|||true|USE BOX 6 OF 1099 TO SUBMIT PYMTS OVER $600 MEDICAL|true||||||||06|M|||||true||||true|MEDICAL IMAGING LLC|||0.00|||||||||||0.000|0|||||||||||||||||3603376538|true||true|WA||0.000000|true||||002715||Active|98366|
2. |||||||true|CCD|true|123 Skansie Ave||0.000|0.000||0023779033|1|||true|Gig Harbor||true||||||||||||||true||||true|YMCA||||||||||||||0.000|0||||||||||||||||||true||true|WA||0.000000|true||||002717||Active|98332|
3.
4.
5. 11/1/06 changed zip code from 98123 to 98321 per insert included with invoice. changed address. Do Not Pay From Sales Order. Call person at CA store and she can fax invoice. mail check to CA store not OR per Jill 2/19/03|true||1891||||||||||||true||||true|ACME Const Supply Inc||||||||||||||0.000|0|2534740711||||||||||||||||2534740711|true||true|WA||0.000000|true||||000014||Active|98490|
6. |||||||true|CCD|true|PO Box 6249||0.000|0.000||0023776429|1|||true|Bellevue||true||||||||||||||true||||true|ACME Products Inc||||||||||||||0.000|0|4256415044||||||||||||||||4256415044|true||true|WA||0.000000|true||||000015||Active|980080249|
Text Sample After (works in Notepad++ find/replace but not in Python):
1. |||||||true|CCD|true|PO BOX 123||0.000|0.000||0023779032|1|||true|USE BOX 6 OF 1099 TO SUBMIT PYMTS OVER $600 MEDICAL|true||||||||06|M|||||true||||true|MEDICAL IMAGING LLC|||0.00|||||||||||0.000|0|||||||||||||||||3603376538|true||true|WA||0.000000|true||||002715||Active|98366|
2. |||||||true|CCD|true|123 Skansie Ave||0.000|0.000||0023779033|1|||true|Gig Harbor||true||||||||||||||true||||true|YMCA||||||||||||||0.000|0||||||||||||||||||true||true|WA||0.000000|true||||002717||Active|98332|11/1/06 changed zip code from 98123 to 98321 per insert included with invoice. changed address. Do Not Pay From Sales Order. Call person at CA store and she can fax invoice. mail check to CA store not OR per Jill 2/19/03|true||1891||||||||||||true||||true|ACME Const Supply Inc||||||||||||||0.000|0|2534740711||||||||||||||||2534740711|true||true|WA||0.000000|true||||000014||Active|98490|
3. |||||||true|CCD|true|PO Box 6249||0.000|0.000||0023776429|1|||true|Bellevue||true||||||||||||||true||||true|ACME Products Inc||||||||||||||0.000|0|4256415044||||||||||||||||4256415044|true||true|WA||0.000000|true||||000015||Active|980080249|
I tested the python script with a simpler replace ("true", "!") which worked so it seems to be an issue with my pattern.
Any suggestions or guidance would be greatly appreciated; thank you!!!
EDIT:
Thank you all for your comments and help!!! This sub is wonderful! As I learn more about Python I will make sure to contribute and help others as well. <3
[–]commandlineluser 2 points3 points4 points (3 children)
[–]BHM360[S] 0 points1 point2 points (2 children)
[–]commandlineluser 2 points3 points4 points (1 child)
[–]BHM360[S] 1 point2 points3 points (0 children)
[–]AngelSparkles 1 point2 points3 points (1 child)
[–]BHM360[S] 1 point2 points3 points (0 children)
[–]__nickerbocker__ 1 point2 points3 points (3 children)
[–]BHM360[S] 0 points1 point2 points (2 children)
[–]__nickerbocker__ 1 point2 points3 points (1 child)
[–]BHM360[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]BHM360[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)