简体   繁体   中英

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?

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+'

s = "a -> b,c;"
s = s.replace(";","")
a = s.split(" -> ")
b = a[1].split(',')
for i in range(len(b)):
    print('{} -> {};'.format(a[0],b[i]))

Read more about Python split and other string methods here . This is only first example, you shall be able to solve the other one by yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM