I am sorry if this question is already answered, but I did not find any. I want to split & convert long strings in multiple strings I have dataframe df:
no strings
1. A_12_234 gef|re1234|gef|re0943
2. O_257363 tef|fe4545|tef|fe3333|tef|9995
I want to make individual strings and create new column
output I am getting:
no strings new_col
1. A_12_234 gef|re1234|gef|re0943 <thekeys db="gef" value="re1234"/>\n<thekeys db="gef" value="re0943"/>
2. O_257363 tef|fe4545|tef|fe3333|tef|9995 <thekeys db="tef" value="fe4545"/>\n<thekeys db="tef" value="fe3333"/>
Desired output:
no strings new_col
1. A_12_234 gef|re1234|gef|re0943 <thekeys db="gef" value="re1234"/>\n<thekeys db="gef" value="re0943"/>
2. O_257363 tef|fe4545|tef|fe3333|tef|9995 <thekeys db="tef" value="fe4545"/>\n<thekeys db="tef" value="fe3333"/>\n<thekeys db="tef" value="9995"/>
I dont know where I am making a mistake, since it is skipping some pairs
Here's code:
def createxm(x):
try:
parsedlist = x['strings'].split('|')
print(parsedlist)
cnt = len(parsedlist)/2
print(cnt)
xm_list = []
for i in range(0, int(cnt), 2):
xm_list.append('<thekeys db="{}" value="{}"/>'.format(parsedlist[i], parsedlist[i+1]))
xm_string = '\n'.join(xml_list)
return xm_string
except:
return None
Thank you