0

I started learning python. I am wondering how to split a list that has two delimiters.

INPUT

1,2,3,4,5;2

My code:

with open(path, 'r') as f:
for fs in f:
    ip= fs.rstrip('\n').split(',')
    print (ip)

My output:

['1', '2', '3', '4', '5;2']

Desired output

['1', '2', '3', '4', '5', '2']

Can i please now how to remove the semicolon in the list.

Thanks

learning fun
  • 519
  • 1
  • 5
  • 12

2 Answers2

1

You can translate all the separators to a single one, say with replace or translate:

with

str.replace(old, new[, max])

You could do this:

print str.replace(";", ",")

and then split

https://www.tutorialspoint.com/python/string_replace.htm

progmatico
  • 4,714
  • 1
  • 16
  • 27
Bruno Dantas
  • 98
  • 2
  • 11
0

Use regex to replace such ; to ,

import re
new_input=re.sub(';',',','1,2,3,4,5;2')
print(new_input.split(','))
mad_
  • 8,121
  • 2
  • 25
  • 40