2024-05-01 15:20:59 发布
网友
我正试图用'\n'替换'\s',但是当我打印line2时,它不会打印用新行替换的空格的行。有人能指出我的语法有什么问题吗?
line2
for line in fi: if searchString in line: line2 = line.replace('\s' , '\n') print line2
\s是正则表达式标记,不会被str.replace理解。
\s
str.replace
做:
line.replace(' ', '\n')
.replace()替换字符串,您需要re.sub(..),例如:
.replace()
re.sub(..)
for line in fi: if searchString in line: line2 = re.sub(r'\s' , '\n', line) print line2
文档有更多详细信息:https://docs.python.org/2/library/re.html#re.sub
\s
是正则表达式标记,不会被str.replace
理解。做:
.replace()
替换字符串,您需要re.sub(..)
,例如:文档有更多详细信息:https://docs.python.org/2/library/re.html#re.sub
相关问题 更多 >
编程相关推荐