用Python根据条件替换字符串。

huangapple go评论108阅读模式
英文:

replace a string with condition in python

问题

嗨,我正在尝试创建一个脚本,用于更改我的模型文件,并且我需要将文本字段类型更改为Bit1BooleanField,当字段类型是一个猜测时。我尝试了这个解决方案,但在替换第一个项目后一切都混乱了。

我的models.py示例:

  1. class BaseCase(models.Model):
  2. base_case_name = models.CharField(primary_key=True, max_length=255)
  3. version = models.CharField(max_length=255)
  4. default = Bit1BooleanField(blank=True, null=True) # 这个字段类型是一个猜测。
  5. class ConfigApiMatrix(models.Model):
  6. bloc = models.CharField(primary_key=True, max_length=255)
  7. page = models.CharField(max_length=255)
  8. activate_model_api = Bit1BooleanField(blank=True, null=True) # 这个字段类型是一个猜测。
  9. module_api_break_point = Bit1BooleanField(blank=True, null=True)
  10. first_api_to_run_after_save = Bit1BooleanField(blank=True, null=True)

我尝试的解决方案:

  1. import re
  2. with open('SFP/models.py', 'r') as myfile:
  3. txt = myfile.read()
  4. word = "Field(blank=True, null=True) # 这个字段类型是一个猜测."
  5. for match in re.finditer(word, txt):
  6. i = match.start()
  7. txt = txt[:i-4-len(txt)] + "Bit1Boolean" + txt[i-len(txt):]
英文:

hi all i'm trying to create script that makes changes to my models file and i need it to change the textfield type to Bit1BooleanField when the field type is a guess
i tried this sultion but after replacing the first item everything is missed up

my models.py sample:

  1. class BaseCase(models.Model):
  2. base_case_name = models.CharField(primary_key=True, max_length=255)
  3. version = models.CharField(max_length=255)
  4. default = TextField(blank=True, null=True) # This field type is a guess.
  5. class ConfigApiMatrix(models.Model):
  6. bloc = models.CharField(primary_key=True, max_length=255)
  7. page = models.CharField(max_length=255)
  8. activate_model_api = models.TextField(blank=True, null=True) # This field type is a guess.
  9. module_api_break_point = models.TextField(blank=True, null=True)
  10. first_api_to_run_after_save = models.TextField(blank=True, null=True)

the solution i tried:

  1. import re
  2. with open('SFP/models.py', 'r') as myfile:
  3. txt = myfile.read()
  4. word = "Field\(blank=True, null=True\) # This field type is a guess."
  5. for match in re.finditer(word, txt):
  6. i=match.start()
  7. txt = txt[:i-4-len(txt)] + "Bit1Boolean" + txt[i-len(txt):]
  8. ``
  9. </details>
  10. # 答案1
  11. **得分**: 1
  12. 这应该是一个适合你的解决方案
  13. ```python
  14. search = 'TextField(blank=True, null=True) # This field type is a guess.'
  15. replace = 'Bit1BooleanField(blank=True, null=True) # This field type is a guess.'
  16. with open('SFP/models.py', "r") as f:
  17. txt = f.read()
  18. replaced_data = txt.replace(search, replace)
  19. with open('SFP/models.py', "w") as f:
  20. f.write(replaced_data)
英文:

This should be a solution for you

  1. search = &#39;TextField(blank=True, null=True) # This field type is a guess.&#39;
  2. replace = &#39;Bit1BooleanField(blank=True, null=True) # This field type is a guess.&#39;
  3. with open(&#39;SFP/models.py&#39;, &quot;r&quot;) as f:
  4. txt = f.read()
  5. replaced_data = txt.replace(search, replace)
  6. with open(&#39;SFP/models.py&#39;, &quot;w&quot;) as f:
  7. f.write(replaced_data)

huangapple
  • 本文由 发表于 2020年1月6日 23:20:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定