[英]Split function in python to saperate string from comma
如何使用 split 将'g5s,12-04-2019'
与逗号分开。
如果a='g5s,12-04-2019'
然后在拆分b = 'g5s'
和c='12-04-1=2019'
后搜索结果
str.split()
进行救援:
a='g5s,12-04-2019'
b, c = a.split(",")
您应该使用x.split()
并将逗号作为输入参数传递,例如x.split(",")
。 这意味着输入参数是分隔符。
split
方法返回一个单独包含元素的列表。 你可以一一解开。 像这样: b, c = a.split(",")
代码:
a = "g5s,12-04-2019"
print("a={a}".format(a=a))
b, c = a.split(",")
print("b={b}, c={c}".format(b=b, c=c))
Output:
>>> python3 test.py
a=g5s,12-04-2019
b=g5s, c=12-04-2019
供参考:
split
方法的文档字符串。
S.split(sep=None, maxsplit=-1) -> 字符串列表
Return a list of the words in S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.