简体   繁体   English

Python将单个元组值拆分为多个值

[英]Python split single tuple value into multiple values

I have the following tuple value and wanted to split single tuple value into multiple values.I tried converting the tuple to string and used split(),splitlines() based on \\n as delimiter however it didn't work.Any inputs please? 我有以下元组值,并想将一个元组值拆分为多个值。我尝试将元组转换为字符串,并使用基于\\ n的split(),splitlines()作为分隔符,但是它不起作用。请输入任何内容吗?

INPUT:
('2018-10-23\n2018-10-25\n2018-10-26\n2018-10-27\n2018-10-28\n2018-10-30\n', 0)

OUTPUT:
2018-10-23
2018-10-25
2018-10-27
2018-10-28
2018-10-30

You need to split the first element of the tuple: 您需要拆分元组的第一个元素:

inpt = ('2018-10-23\n2018-10-25\n2018-10-26\n2018-10-27\n2018-10-28\n2018-10-30\n', 0)

result = inpt[0].strip().split()

for e in result:
    print(e)

Output 输出量

2018-10-23
2018-10-25
2018-10-26
2018-10-27
2018-10-28
2018-10-30

If you want a list, you can use str.splitlines . 如果需要列表,可以使用str.splitlines If you want the output you posted on the screen... just print it. 如果要在屏幕上发布输出,则只需打印即可。

>>> inp = ('2018-10-23\n2018-10-25\n2018-10-26\n2018-10-27\n2018-10-28\n2018-10-30\n', 0)
>>> 
>>> inp[0].splitlines()
['2018-10-23',
 '2018-10-25',
 '2018-10-26',
 '2018-10-27',
 '2018-10-28',
 '2018-10-30']
>>> 
>>> print(inp[0], end='')
2018-10-23
2018-10-25
2018-10-26
2018-10-27
2018-10-28
2018-10-30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM