使用[]python在文本文件中写入

使用[]python在文本文件中写入,python,python-2.7,Python,Python 2.7,我正试图用大括号写一个文本文件 output.write("{}\t{}\t{}\t{}\t{}\t".format(student_iD,user_id,First_name, Last_name,Other_name)) output.write("\n") 它写入文本文件时不带大括号。我应该添加什么才能得到这样的结果: [student_id] [user_id] [First_name] [Last_name] [other_name] in the text file. 用

我正试图用大括号写一个文本文件

output.write("{}\t{}\t{}\t{}\t{}\t".format(student_iD,user_id,First_name, Last_name,Other_name))
output.write("\n")
它写入文本文件时不带大括号。我应该添加什么才能得到这样的结果:

[student_id]  [user_id]  [First_name]  [Last_name]  [other_name] in the text file.

[]

output.write("[{}]\t[{}]\t[{}]\t[{}]\t[{}]\t".format(student_iD,user_id,First_name, Last_name,Other_name))
output.write("\n")
就这么简单:

output.write("[{}]\t[{}]\t[{}]\t[{}]\t[{}]\t".format(...)
或者,在不更改格式字符串的情况下,将括号括在变量名本身周围。然后您将打印列表,每个列表将只包含一个值

output.write("{}\t{}\t{}\t{}\t{}\t".format([student_iD],[user_id], [First_name], [Last_name], [Other_name]))
我的代码

output = open('/home/vipul/Desktop/test_data.txt', 'a')
output.write("[{}]\t[{}]\t[{}]\t[{}]\t[{}]\t".format(222,2222,'test',     'test','test'))
output.write("\n")
我得到的输出: [222][2222][test][test][test]


它起作用了。如果出现错误,请将输出粘贴到此处

括号!抱歉是迂腐的:考虑在括号周围放置括号,像这样的代码> [{}] 。括号起到占位符的作用,用于在将内容写入文件之前对字符串应用格式化。将它们括在括号中以获得括号。这在我的情况下不起作用:(@user3419487,它不起作用了??你尝试了什么?你犯了什么错误/行为不当?没有错误……但它只是不带大括号地打印。@user3419487,你得到了什么输出?我得到了不带大括号的输出。student\u id user\u id First\u name Last\u name other_name@user3419487,不可能。您可能忘记了关闭()或
flush()
输出.输出.写入(“[{}]\t[{}]\t[{}]\t{}\t.”格式([student\u id],[user\u id],[First\u name],[Last\u name],Other\u name])输出.写入(“\n”输出我得到:26615188 0000-0002-9903-4248迈克尔是['mbaudis']为什么你要以格式给出[]波特()方法也?只在一个地方给出它。现在在字符串中给出它。一个大括号没有用大括号括起来。你得到的输出是什么?粘贴在这里。我已经尝试了各种方法…我猜我看到了一些奇怪的问题。我在我的机器中得到了你想要的精确输出。在2.x和3.x中。请在你需要时粘贴你得到的输出运行代码。它对一些输出有效。但不是全部。我得到的输出:['2014232'['0000-0002-3952-3285']['Des']['Higgins'][['Des Higgins']]