python - split the string and print in separate variable

hdrajani Source

I want to split a string and print it in two separate variables, what i am doing is:

string = [['SMC/BTC', 3624, 6.31e-06, 6.18e-06],['SKY/BTC', 5038, 0.00212768, 0.00207798],['SQL/BTC', 1596, 0.00011456, 0.00010204]]
a = string[0][0].partition("/")[0]
b = string[0][0].partition("/")[2]
print(a,b)

it is showing error:

a = string[0][0].partition("/")[0]
TypeError: 'float' object is not subscriptable

I have no idea how to handle this error, any help would be appreciated. Thanks.

pythonpython-3.x

Answers

answered 6 months ago Rakesh #1

You can use the split method.

string = [['SMC/BTC', 3624, 6.31e-06, 6.18e-06],['SKY/BTC', 5038, 0.00212768, 0.00207798],['SQL/BTC', 1596, 0.00011456, 0.00010204]]
for i in string:
    if "/" in i[0]:
        a, b =  i[0].split("/")
        print(a, b)

output:

SMC BTC
SKY BTC
SQL BTC

comments powered by Disqus