veilupearl
veilupearl

Reputation: 929

Match multiple lines until a string contains

I have an csv file which contains the following information and I need the regular expression matching with the string as 'B08-1506' starting point until the next pattern matching with the above string. And I want to append the three lines to be considered as a single line

B08-1506,324873, st, $0.0,
ljkflka,,,,,
1 of 37 jksdfhjfhjk
jkdsfh,,,,,,,
B08-1606,324873, st, $0.0,
ljkflka,,,,,
1 of 37 jksdfhjfhjk
jkdsfh,,,,,,,
B09-0680,324873, st, $0.0,
ljkflka,,,,,
1 of 37 jksdfhjfhjk
jkdsfh,,,,,,,
B09-0681,324873, st, $0.0,
ljkflka,,,,,
1 of 37 jksdfhjfhjk
jkdsfh,,,,,,,

Output should be like this,

B08-1506,324873, st, $0.0,ljkflka,jksdfhjfhjk,jkdsfh
B08-1606,324873, st, $0.0,ljkflka,jksdfhjfhjk,jkdsfh
B09-0680,324873, st, $0.0,ljkflka,jksdfhjfhjk,jkdsfh
B09-0681,324873, st, $0.0,ljkflka,jksdfhjfhjk,jkdsfh

Upvotes: 0

Views: 199

Answers (1)

Rakesh
Rakesh

Reputation: 82765

Like Nisarg said it is best to fix the source csv format. But incase you are not able to the below snippet might help.

Demo:(Without Regex)

s = """B08-1506,324873, st, $0.0,
ljkflka,,,,,
1 of 37 jksdfhjfhjk
jkdsfh,,,,,,,
B08-1606,324873, st, $0.0,
ljkflka,,,,,
1 of 37 jksdfhjfhjk
jkdsfh,,,,,,,
B09-0680,324873, st, $0.0,
ljkflka,,,,,
1 of 37 jksdfhjfhjk
jkdsfh,,,,,,,
B09-0681,324873, st, $0.0,
ljkflka,,,,,
1 of 37 jksdfhjfhjk
jkdsfh,,,,,,,"""

res = []
for i in s.split("\n"):
    if i.startswith("B0"):    #Check if line starts with "B0"
        res.append(i)
    else:                      #else concat to the previous element in res. 
        res[-1] = res[-1]+i

res = [filter(None, i.split(",")) for i in res]    #Filter to remove all empty elements
for i in res:
    print(", ".join(i))

Output:

B08-1506, 324873,  st,  $0.0, ljkflka, 1 of 37 jksdfhjfhjkjkdsfh
B08-1606, 324873,  st,  $0.0, ljkflka, 1 of 37 jksdfhjfhjkjkdsfh
B09-0680, 324873,  st,  $0.0, ljkflka, 1 of 37 jksdfhjfhjkjkdsfh
B09-0681, 324873,  st,  $0.0, ljkflka, 1 of 37 jksdfhjfhjkjkdsfh

Upvotes: 1

Related Questions