[英]How to add space before or after if there is no space for a particular word in python without regex
[英]How to add space before a particular string in python
从字符串中删除所有空格后,我有以下输出
テレビを付けて
テレビつけて
つけて
テレビをオンにして
但是,我试图在を字符之后或つけて之后的つけて之前添加一个空格。
所需的输出如下
テレビを 付けて
テレビ つけて
つけて
テレビを オンにして
我尝试使用一些def函数,但不确定如何完成它,即使它能正常工作。
def teform(txt):
if x = "オンにして":
return " して"
elif y = "つけて":
return " つけて"
elif z = "付けて":
return " 付けて"
else:
return # ...(not sure what goes here)
您不能使用=
来比较项目,必须使用==
。
除了错误的比较语法之外,您似乎并不真正知道该如何处理–您不需要任何循环。 只需使用txt.replace
定位并更改那些特定的子字符串即可:
def teform(txt):
# add a space after を
txt = txt.replace ('を','を ')
# add a space between テレビ and つけて
txt = txt.replace ('テレビつけて', 'テレビ つけて')
return txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.