我有一个长字符串,我用一堆计算值构建.然后我将此字符串写入文件.我把它格式化为:
string = str(a/b)+\
'\t'+str(c)\
'\t'+str(d)\
...
'\n'
Run Code Online (Sandbox Code Playgroud)
我想对每个值所代表的内容添加注释,但是评论#或'''不起作用.这是一个例子:
string = str(a/b)+\ #this value is something
'\t'+str(c)\ #this value is another thing
'\t'+str(d)\ #and this one too
...
'\n'
Run Code Online (Sandbox Code Playgroud)
我发现它不起作用:)所以我想知道在这种情况下,带有干净语法的代码会是什么样子.
对我来说,唯一的选择就是string +=在每一条线上行动,但我正在摸索着"必须有更好的方法".
一个简单的解决方案是使用括号:
string = (str(a/b)+ #this value is something
'\t'+str(c)+ #this value is another thing
'\t'+str(d)+ #and this one too
...
'\n')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
266 次 |
| 最近记录: |