Python中的字符串分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4272784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:01:09  来源:igfitidea点击:

String Delimiter in Python

python

提问by anupash

I want to do split a string using "},{"as the delimiter. I have tried various things but none of them work.

我想使用"},{"作为分隔符来拆分字符串。我尝试了各种事情,但没有一个奏效。

string="2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3 "

Split it into something like this:

把它拆分成这样的:

2,1,6,4,5,1
8,1,4,9,6,6,7,0
6,1,2,3,9
2,3,5,4,3

string.split("},{")works at the Python console but if I write a Python script in which do this operation it does not work.

string.split("},{")在 Python 控制台上工作,但如果我编写一个 Python 脚本来执行此操作,则它不起作用。

回答by duffymo

You could do it in steps:

您可以分步骤进行:

  1. Split at commas to get "{...}" strings.
  2. Remove leading and trailing curly braces.
  1. 以逗号分割以获得“{...}”字符串。
  2. 删除前导和尾随花括号。

It might not be the most Pythonic or efficient, but it's general and doable.

它可能不是最 Pythonic 或最高效的,但它是通用且可行的。

回答by Gavin H

You need to assign the result of string.split("},{")to a new string. For example:

您需要将结果分配string.split("},{")给一个新字符串。例如:

string2 = string.split("},{")

I think that is the reason you think it works at the console but not in scripts. In the console it just prints out the return value, but in the script you want to make sure you use the returned value.

我认为这就是您认为它在控制台上工作而不是在脚本中工作的原因。在控制台中它只是打印出返回值,但在脚本中您要确保使用返回值。

回答by John Kugelman

You need to returnthe string back to the caller. Assigning to the stringparameter doesn't change the caller's variable, so those changes are lost.

您需要return将字符串返回给调用者。分配给string参数不会更改调用者的变量,因此这些更改将丢失。

def convert2list(string):
    string = string.strip()
    string = string[2:len(string)-2].split("},{")

    # Return to caller.
    return string

# Grab return value.
converted = convert2list("{1,2},{3,4}")

回答by anupash

I was taking the input from the console in the form of arguments to the script....

我正在以脚本参数的形式从控制台获取输入....

So when I was taking the input as {{2,4,5},{1,9,4,8,6,6,7},{1,2,3},{2,3}} it was not coming properly in the arg[1] .. so the split was basically splitting on an empty string ...

因此,当我将输入视为 {{2,4,5},{1,9,4,8​​,6,6,7},{1,2,3},{2,3}} 时,它不是在 arg[1] 中正确出现 .. 所以分裂基本上是在一个空字符串上分裂......

回答by praba230890

If I run the below code from a script file (in Python 2.7):

如果我从脚本文件(在 Python 2.7 中)运行以下代码:

string="2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3 "

print string.split("},{")

string="2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3 ,5,4,3"

打印 string.split("},{")

Then the output I got is:

然后我得到的输出是:

['2,1,6,4,5,1', '8,1,4,9,6,6,7,0', '6,1,2,3,9', '2,3,5,4,3 ']

['2,1,6,4,5,1', '8,1,4,9,6,6,7,0', '6,1,2,3,9', '2,3, 5,4,3']

And the below code also works fine:

下面的代码也可以正常工作:

string="2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3 "
def convert2list(string):
    string=string.strip()
    string=string[:len(string)].split("},{")
    print string
convert2list(string)

回答by Hunter

Use This: This will split the string considering },{as a delimiter and print the list with line breaks.

使用此:这将拆分字符串,将},{作为分隔符并打印带换行符的列表。

string = "2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3"
for each in string.split('},{'):
    print each

Output:

输出:

2,1,6,4,5,1
8,1,4,9,6,6,7,0
6,1,2,3,9
2,3,5,4,3

If you want to print the split items in the list only you can use this simple print option.

如果您只想打印列表中的拆分项目,您可以使用这个简单的打印选项。

string = "2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3"
print string.split('},{')

Output:

输出:

['2,1,6,4,5,1', '8,1,4,9,6,6,7,0', '6,1,2,3,9', '2,3,5,4,3']