Replacing unquoted words only in Python

后端 未结 6 855
情深已故
情深已故 2021-01-21 04:03

I am looking for a way to replace a word, however only when its not surounded by quotations.

For example Replacing Hello with Hi

6条回答
  •  猫巷女王i
    2021-01-21 04:29

    Consider using a regular expression (not the only way, but I'd go with that).

    In [2]: print s
    Hello 'Hello' Nothing
    In [3]: import re
    In [4]: re.sub("(?

提交回复
热议问题