簡體   English   中英

Python中的換行注釋是否可行?

[英]Is line wrap comment possible in Python?

我有一個長字符串,我用一堆計算值構建。 然后我將此字符串寫入文件。 我把它格式化為:

string = str(a/b)+\
         '\t'+str(c)\
         '\t'+str(d)\
         ...
         '\n' 

我想對每個值表示的內容添加注釋,但使用#'''注釋不起作用。 這是一個例子:

string = str(a/b)+\     #this value is something
         '\t'+str(c)\   #this value is another thing
         '\t'+str(d)\   #and this one too
         ...
         '\n'

我發現它不起作用:)所以我想知道在這種情況下,帶有干凈語法的代碼會是什么樣子。

對我來說,唯一的選擇就是在每一行上找到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')

怎么樣

string = '\t'.join(map(str,((a/b),    #this value is something
                            c,        #this value is another thing
                            d,        #and this one too
                            )))+'\n'

或者如果你願意的話

string = '\t'.join(map(str,(
        (a/b),    #this value is something
        c,        #this value is another thing
        d,        #and this one too
        )))+'\n'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM