Kartik Lakshminarasimhan Kartik Lakshminarasimhan - 3 months ago 11
Python Question

python: selectively split and combine alphanumeric strings in a file

I am trying to replicate certain lines in a file and strip certain substrings from certain replicas. Each line is a string which I get through a fileobject.readline()

Input:
Line1 : a -> b,c;
Line2 : d -> e,f,g;
Line3 : h -> i,j;
Line4 : k -> l;

Output:
Line1 : a -> b;
Line2 : a -> c;
Line3 : d -> e;
Line4 : d -> f;
Line5 : d -> g;
Line6 : h -> i;
Line7 : h -> j;
Line8 : k -> l;


In the output I want replicate the input line(a single string) to multiple lines(multiple strings) selectively .
Number of replicas/duplicates:
For n "," characters in a line, I replicate the lines (n +1) times
where, n starts from 1,
Strings to be put in replicas/duplicates:
replica 1: substring1 ->substring2[0]
replica 2: substring1 -> substring2[1]

and so on.

Could anyone please help me with the above problem?

Answer

As far as I understand your question, you can simply replace ',' characters with the '\w ' pattern. It's more like a trick than a solution.

I wrote a simple function that can 'replicate' a line into multiple line follows your requirement. Note that this is for 1 line only.

def replicateALine(line):
    regex = re.search('\w -> ', line)
    if regex:
        line= line.replace(',', ';\n' + regex.group(0))
    return line

print replicateALine('a -> b,c,d;')
print replicateALine('e -> b;')

output:

a -> b;
a -> c;
a -> d;
e -> b;

Update: Because \w will only cover 1 character, if you need to cover things like snu_1opbb_1, try '\w+'

Comments