繁体   English   中英

在 python 中拆分 function 以将字符串与逗号分开

[英]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.

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