在Python中使用f字符串的变量和空格。

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

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

huangapple
  • 本文由 发表于 2023年1月9日 19:03:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056346.html
匿名

发表评论

匿名网友

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

确定