簡體   English   中英

格式字符串python,帶有不同數量的占位符

[英]format string python with varying number of placeholders

    def SumFunc(E,n):
        write_path = open('somepath.text','a')
        some function here evaluates the following...
        c1 = 
        c2 = 
        c4 = 
        c4 =  
        write_path.write('{} {} {} {} {}\n'.format(E,c1,c2,c3,c4))

我有一個函數,取決於'E''n' ,其中'n'是最后的占位符數量,在這種情況下為5。對於不同的示例,我的'n'從4到100不等。

我的問題是如何格式化文件上的文字,使其適用於所有'n'格式。 像在n = 4中一樣,它使用4 占位符寫入,而在n = 50中時,它將創建50個占位符並寫入50個值。

謝謝

將您的值(Cs)存儲在列表中,我們稱它為c_list

然后以這種方式生成您的字符串:

string = ' '.join([str(x) for x in c_list]) + '\n'
write_path.write(string)

暫無
暫無

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

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