首页 > 解决方案 > 如何在 Python 3 中使用组合的格式化字符串文字和三引号来修复代码?

问题描述

我正在编写一个脚本来自动进行一些网格划分,并且需要以某种格式输出一个文本文件。我在三引号之前使用了 F 但它说在代码末尾关闭 file.write() 时出现语法错误。不知道为什么。提前致谢。



with open('/Users/aben/blockMeshDict','w') as file:
    file.write(F'''

FoamFile
{{
    version     2.0;
    format      ascii;
    class       dictionary;
    object      blockMeshDict;
}}


convertToMeters 1;

vertices
(
    (0 0 -0.1)
    (0.6 0 -0.1)
    (0 0.2 -0.1)
    (0.6 0.2 -0.1)
    (3 0.2 -0.1)
    (0 1 -0.1) 
    (0.6 1 -0.1) 
    (3 1 -0.1) 
    (0 0 0.1) 
    (0.6 0 0.1) 
    (0 0.2 0.1) 
    (0.6 0.2 0.1) 
    (3 0.2 0.1)
    (0 1 0.1) 
    (0.6 1 0.1)
    (3 1 0.1)
);

blocks
(
    hex (0 1 3 2 8 9 11 10) ({x0} {y0} 1) simpleGrading (1 1 1)
    hex (2 3 6 5 10 11 14 13) ({x1} {y1} 1) simpleGrading (1 1 1)
    hex (3 4 7 6 11 12 15 14) ({x2} {y2} 1) simpleGrading (1 1 1)
);''')

标签: pythonpython-3.x

解决方案


推荐阅读