在Python中,如果空格被引号包围,如何替换字符串中的所有空格?
我有一张单子在Python中,如果空格被引号包围,如何替换字符串中的所有空格?,python,regex,string,list,replace,Python,Regex,String,List,Replace,我有一张单子 l=[“这是”,“第一个”字符串”,“和“它是好的”] 我想将s内字符串中的所有空格替换为“| space |” print (l) # ["This is","'the|space|first|space|'string","and 'it|space|is|space|'good"] 我不能在for循环中使用for循环并直接使用.replace(),因为字符串是不可变的 TypeError:“str”对象不支持项分
l=[“这是”,“第一个”字符串”,“和“它是好的”]
我想将s内字符串中的所有空格替换为“| space |”
print (l)
# ["This is","'the|space|first|space|'string","and 'it|space|is|space|'good"]
我不能在for循环中使用for循环并直接使用.replace(),因为字符串是不可变的
TypeError:“str”对象不支持项分配
我看到了下面的问题,但没有一个对我有帮助
替换循环Python中的字符串元素(3个答案)
在for循环中运行replace()方法?(3个答案)
使用列表理解替换字符串(7个答案)
我已经考虑过使用re.sub,但是想不出一个合适的正则表达式来完成这项工作。我想我已经用regex解决了您的替换问题。您可能需要对给定的代码片段进行进一步润色,以满足您的需要 如果我正确理解了这个问题,那么诀窍就是使用正则表达式找到要替换的正确空间
match = re.findall(r"\'(.+?)\'", k) #here k is an element in list.
放置骨架代码以供参考:
import re
l = ["This is","'the first 'string","and 'it is 'good"]
#declare output
for k in l:
match = re.findall(r"\'(.+?)\'", k)
if not match:
#append k itself to your output
else:
p = (str(match).replace(' ', '|space|'))
#append p to your output
我还没有测试过,但应该可以用。如果您在这方面遇到任何问题,请告诉我。我想我已经用正则表达式解决了您的替换问题。您可能需要对给定的代码片段进行进一步润色,以满足您的需要 如果我正确理解了这个问题,那么诀窍就是使用正则表达式找到要替换的正确空间
match = re.findall(r"\'(.+?)\'", k) #here k is an element in list.
放置骨架代码以供参考:
import re
l = ["This is","'the first 'string","and 'it is 'good"]
#declare output
for k in l:
match = re.findall(r"\'(.+?)\'", k)
if not match:
#append k itself to your output
else:
p = (str(match).replace(' ', '|space|'))
#append p to your output
我还没有测试过,但应该可以用。如果您在这方面遇到任何问题,请告诉我。这对我很有用:
>>> def replace_spaces(str) :
... parts = str.split("'")
... for i in range(1,len(parts),2) :
... parts[i] = parts[i].replace(' ', '|')
... return "'".join( parts )
...
>>> [replace_spaces(s) for s in l]
['This is', "'the|first|'string", "and 'it|is|'good"]
>>>
这对我很有用:
>>> def replace_spaces(str) :
... parts = str.split("'")
... for i in range(1,len(parts),2) :
... parts[i] = parts[i].replace(' ', '|')
... return "'".join( parts )
...
>>> [replace_spaces(s) for s in l]
['This is', "'the|first|'string", "and 'it|is|'good"]
>>>
使用
regex文本蒙格
:
import re
l = ["This is","'the first 'string","and 'it is 'good"]
def repl(m):
return m.group(0).replace(r' ', '|space|')
l_new = []
for item in l:
quote_str = r"'.+'"
l_new.append(re.sub(quote_str, repl, item))
print(l_new)
输出:
['This is', "'the|space|first|space|'string", "and 'it|space|is|space|'g
ood"]
完整的逻辑基本上是:
l
的元素李>
字符串。将其传递给repl
函数李>
repl
函数我正在使用simplereplace
将空格替换为|空格
使用regex文本蒙格的文本蒙格引用=>:
import re
l = ["This is","'the first 'string","and 'it is 'good"]
def repl(m):
return m.group(0).replace(r' ', '|space|')
l_new = []
for item in l:
quote_str = r"'.+'"
l_new.append(re.sub(quote_str, repl, item))
print(l_new)
输出:
['This is', "'the|space|first|space|'string", "and 'it|space|is|space|'g
ood"]
完整的逻辑基本上是:
循环通过l
的元素李>
在单引号之间查找字符串。将其传递给repl
函数李>
repl
函数我正在使用simplereplace
将空格替换为|空格
文本搜索的参考=>我很确定正则表达式在这种情况下不会起作用。我认为使用引号作为分隔符拆分字符串比使用a字符串替换更容易。我很确定正则表达式在这种情况下不起作用。我认为使用引号作为分隔符拆分字符串比使用a字符串替换更容易。感谢您提供了简单的解决方案!真不敢相信我竟然没有想到这一点。谢谢你的简单解决方案!真不敢相信我居然没想到这个。