如何忽略单词中的逗号并将其 return 作为字符串中的一个单词

how to ignore commas inside a word and return it as one word in a string

我有一个字符串,其中包含以逗号分隔的单词。在该字符串中有一个名称 "v,i,n,t,a,g,e",字符之间用逗号写成。我想知道如何告诉 python 这是字符串中的一个词而不是单独的词。

例如

s= 'the new singer v,i,n,t,a,g,e is very famous'

我已经尝试 split(/(?!\d)\,(?!\d)/) 来表示数字中的逗号,例如 100,000,但我不知道该怎么做。

我要:

s= "'the', 'new', 'singer', 'v,i,n,t,a,g,e' , 'is', 'very', 'famous'"

您可以使用split()函数:

s= 'the new singer v,i,n,t,a,g,e is very famous'
result = s.split(' ')

输出:

['the', 'new', 'singer', 'v,i,n,t,a,g,e', 'is', 'very', 'famous']

否则,如果您只是 "have a string that contains words separated by comma",并且每个字符之间用逗号分隔几个单词,您可以这样做:

s= 'the,new singer,v,i,n,t,a,g,e,is,very,famous'
result = []
hidden_word = ''
for string in s.split(','):
    if len(string)>1:
        if len(hidden_word)>0:
            result.append(hidden_word)
            hidden_word = ''
        result.append(string)
    else:
        hidden_word += string

输出:

['the', 'new singer', 'vintage', 'is', 'very', 'famous']

此方法的一个优点是您得到的是 "vintage" 而不是 "v,i,n,t,a,g,e"

>>> s = 'the new singer v,i,n,t,a,g,e is very famous'
>>> ', '.join(f"'{x}'" for x in s.split(' '))
"'the', 'new', 'singer', 'v,i,n,t,a,g,e', 'is', 'very', 'famous'"