Skip to main content
 首页 » 编程设计

python之写入没有引号的文件,Python 中没有逗号

2025年05月04日9三少

我有以下使用 Python 将输出写入文件的代码。

ID= # type is <class 'str'> 
dictionary={} #Dictionary 
 
    for item in dictionary.keys(): 
        output=str([Question_ID,item,dictionary[item]]) 
        target = open('result.relevancy', 'a') 
        target.write(output+'\n') 

输出文件创建如下:

['Q1', 'R6', 0.08] 

我只想输出纯文本字符,如下所示:

Q1    R6   0.08 

(没有引号,逗号括号)

请您参考如下方法:

您可以轻松地将字符串格式化为您想要的任何格式:

output="%s %s %s" % (Question_ID, item, dictionary[item]) 

您可以在 https://pyformat.info/ 中找到更多信息