Split string using python and regex -
i have following csv file:
name details abc type1: y, property: p1,p3 , type2:n def type1: y, property: p2,p3 , type2:n ghi type1: n, property: p1,p2 , type2:y jkl type1: n, property: p1,p3 , type2:y
i want have ouput file as:
name type1 property type2 abc y p1,p3 n def y p2,p3 n ghi n p1,p2 y jkl n p1,p3 y
using python , regular expressions, if split details column based on ',' property type splits separate columns. there way deal situation?
there many way this, split each line on punctuation/whitespace character, , reconstruct manually based on desire:
import re t = """abc type1: y, property: p1,p3 , type2:n def type1: y, property: p2,p3 , type2:n ghi type1: n, property: p1,p2 , type2:y jkl type1: n, property: p1,p3 , type2:y""".split('\n') x in t: y = re.findall(r"[\w']+", x) #print y print '\t'.join((y[0],y[2],y[4]+','+y[5],y[7])) > abc y p1,p3 n > def y p2,p3 n > ghi n p1,p2 y > jkl n p1,p3 y
another way without regex replace delimiting characters , reconstruct automatically. this:
print [x.replace(':','\t').replace(' , ','\t').split() x in t]
Comments
Post a Comment