python - 如何在 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)
);''')
解决方案
推荐阅读
- java - 在代码堵塞中的基本代码中出现运行时错误
- java - 使用Java 8流比较两个列表相同位置的元素
- php - 使 PHP 会话更长
- ruby-on-rails - 使用jBuilder构建复杂数组
- ruby-on-rails - Rails 如何在 ISO-8859-1 中响应 JSON
- assembly - Atmega8515 avr usart、avr内存、avr cpu循环模拟日志
- flutter - ListBody 中的空值
- android - 杀死应用程序时将数据保存到 Android SQLite 数据库
- java - 如何通过 Java 输出控制台运行 GDB 并接受 GDB 命令?
- python - 大熊猫连续年份总和的差异