英文:
Use of variable and space in f string in Python
问题
我试图将文件中的一个值替换为一些空格和一个变量的值,但我无法在f字符串中正确替换它。
代码:
f = open("C:\\Users\\dd00849401\\Documents\\yamls\\reg.yaml", 'r')
filter_exp = 'True'
newf = f.read().replace(f'filter_sql_expr: {filter_exp}', f"filter_sql_expr:|- \n {filter_exp}")
f.close()
with open('C:\\Users\\dd00849401\\Documents\\yamls\\updated_test.yaml', 'w') as ff:
ff.write(newf)
ff.close()
输出:
row_filters:
NONE:
filter_sql_expr: True
期望的输出:
row_filters:
NONE:
filter_sql_expr:|-
True
如何修改替换函数中的f字符串,以便它可以读取空格和变量的值?
英文:
I am trying to replace a value in a file with some space and a value from a variable but I am unable to get the right replace with f string in it
reg.yaml
row_filters:
NONE:
filter_sql_expr: True
rule_dimensions:
- consistency
- correctness
- duplication
- completeness
- conformance
- integrity
- accuracy
Code:
f = open("C:\\Users\\dd00849401\\Documents\\yamls\\reg.yaml", 'r')
filter_exp = 'True'
newf = f.read().replace(f'filter_sql_expr: {filter_exp}', f"""filter_sql_expr:|- '' '' '' '' '' '' '' '' '' ''{filter_exp}""")
f.close()
with open('C:\\Users\\dd00849401\\Documents\\yamls\\updated_test.yaml', 'w') as ff:
ff.write(newf)
ff.close()
output:
row_filters:
NONE:
filter_sql_expr: True
Expected:
row_filters:
NONE:
filter_sql_expr:|-
True
How can modify the f string in the replace function so that it can read both the spaces and variable.
答案1
得分: 0
尝试替换
newf = f.read().replace(f'filter_sql_expr: {filter_exp}',
f"filter_sql_expr:|-\n {filter_exp}")
运行良好:
$ cat test
row_filters:
NONE:
filter_sql_expr: True
rule_dimensions:
- consistency
- correctness
- duplication
- completeness
- conformance
- integrity
- accuracy
$ nano test.py # 原始版本
$ python test.py
$ cat out
row_filters:
NONE:
filter_sql_expr:|-
True
rule_dimensions:
- consistency
- correctness
- duplication
- completeness
- conformance
- integrity
- accuracy
$ nano test.py # 修复版本
$ python test.py
$ cat out
row_filters:
NONE:
filter_sql_expr:|-
True
rule_dimensions:
- consistency
- correctness
- duplication
- completeness
- conformance
- integrity
- accuracy
英文:
Try to replace
newf = f.read().replace(f'filter_sql_expr: {filter_exp}', f"""filter_sql_expr:|- '' '' '' '' '' '' '' '' '' ''{filter_exp}""")
with
newf = f.read().replace(f'filter_sql_expr: {filter_exp}',
f"filter_sql_expr:|-\n {filter_exp}")
Works well:
$ cat test
row_filters:
NONE:
filter_sql_expr: True
rule_dimensions:
- consistency
- correctness
- duplication
- completeness
- conformance
- integrity
- accuracy
$ nano test.py # original version
$ python test.py
$ cat out
row_filters:
NONE:
filter_sql_expr:|- '' '' '' '' '' '' '' '' '' ''True
rule_dimensions:
- consistency
- correctness
- duplication
- completeness
- conformance
- integrity
- accuracy
$ nano test.py # fixed version
$ python test.py
$ cat out
row_filters:
NONE:
filter_sql_expr:|-
True
rule_dimensions:
- consistency
- correctness
- duplication
- completeness
- conformance
- integrity
- accuracy
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论