如何拆分字符串并将其存储在临时变量中

时间:2017-03-31 10:16:37

标签: python django python-2.7 python-3.x tornado

我有一个数据,这只是一个字符串 b'365,7,7675962680,4040 。我想分割这些数据并且只想存储 7675962680 任何临时变量。我不知道,如何在python中拆分和选择特定数据。我有一个小代码,请帮我解决这些问题

manage.py

def data(self, data):
    data1 = data # b'365,7,7675962680, 4040
    # want to split these and store 7675962680 in a variable

3 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

def get_data(data):
    data = data.decode()
    data_list = data.split(',')
    return data_list[2]

a = get_data(b'365,7,7675962680, 4040')
print(a)
>> 7675962680

答案 1 :(得分:0)

假设,

s ="365,7,7675962680, 4040"
s.split(',')[2]
'7675962680'

答案 2 :(得分:0)

def data(self, data):
    # data = b'365,7,7675962680, 4040
    # data MUST be converted to str before splitting it
    data1 = data.decode('utf8').split(',')[2]
    print(data1)  # 7675962680
    print(type(data1))  # str