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

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

replace a string with condition in python

问题

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

我的models.py示例:

class BaseCase(models.Model):
    base_case_name = models.CharField(primary_key=True, max_length=255)
    version = models.CharField(max_length=255)
    default = Bit1BooleanField(blank=True, null=True)  # 这个字段类型是一个猜测。


class ConfigApiMatrix(models.Model):
    bloc = models.CharField(primary_key=True, max_length=255)
    page = models.CharField(max_length=255)
    activate_model_api = Bit1BooleanField(blank=True, null=True)  # 这个字段类型是一个猜测。
    module_api_break_point = Bit1BooleanField(blank=True, null=True)
    first_api_to_run_after_save = Bit1BooleanField(blank=True, null=True)

我尝试的解决方案:

import re

with open('SFP/models.py', 'r') as myfile:
    txt = myfile.read()

word = "Field(blank=True, null=True)  # 这个字段类型是一个猜测."
for match in re.finditer(word, txt):
    i = match.start()
    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:

class BaseCase(models.Model):
    base_case_name = models.CharField(primary_key=True, max_length=255)
    version = models.CharField(max_length=255)
    default = TextField(blank=True, null=True)  # This field type is a guess.


class ConfigApiMatrix(models.Model):
    bloc = models.CharField(primary_key=True, max_length=255)
    page = models.CharField(max_length=255)
    activate_model_api = models.TextField(blank=True, null=True)  # This field type is a guess.
    module_api_break_point = models.TextField(blank=True, null=True)
    first_api_to_run_after_save = models.TextField(blank=True, null=True)

the solution i tried:

import re

with open('SFP/models.py', 'r') as myfile:
  txt = myfile.read()

word = "Field\(blank=True, null=True\)  # This field type is a guess."
for match in re.finditer(word, txt):
    i=match.start()
    txt = txt[:i-4-len(txt)] + "Bit1Boolean" + txt[i-len(txt):]
``
    

</details>


# 答案1
**得分**: 1

这应该是一个适合你的解决方案

```python
search = 'TextField(blank=True, null=True)  # This field type is a guess.'
replace = 'Bit1BooleanField(blank=True, null=True)  # This field type is a guess.'

with open('SFP/models.py', "r") as f:
  txt = f.read()
  replaced_data = txt.replace(search, replace)
with open('SFP/models.py', "w") as f:
  f.write(replaced_data)
英文:

This should be a solution for you

search = &#39;TextField(blank=True, null=True)  # This field type is a guess.&#39;
replace = &#39;Bit1BooleanField(blank=True, null=True)  # This field type is a guess.&#39;

with open(&#39;SFP/models.py&#39;, &quot;r&quot;) as f:
  txt = f.read()
  replaced_data = txt.replace(search, replace)
with open(&#39;SFP/models.py&#39;, &quot;w&quot;) as f:
  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:

确定