用[] python写在文本文件中

时间:2016-08-23 09:11:36

标签: 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.

3 个答案:

答案 0 :(得分:0)

[]

围绕这些大括号
output.write("[{}]\t[{}]\t[{}]\t[{}]\t[{}]\t".format(student_iD,user_id,First_name, Last_name,Other_name))
output.write("\n")

答案 1 :(得分:0)

就这么简单:

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]))

答案 2 :(得分:0)

我的代码

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] [测试] [测试] [测试]

它的工作。如果您在此处粘贴输出时出错。