2013-12-08 59 views
1

我想覆盖文件名称student_information.txt。我曾尝试:用Python编写文件

attribute_name = ["student_id", "name", "department", "sex", "age", "nationality",      "grade"] 
attribute_type = ["INT", "CHAR(20)", "CHAR(50)", "CHAR(1)", "INT", "CHAR(3)", "FLOAT"] 
student_list = [("student_id", "name", "department", "sex", "age", "nationality", "grade"), ("student_id", "name", "department", "sex", "age", "nationality", "grade")] 
f = open("student_information.txt", "w") 
f.write('\n'.join('%s %s %s %s %s %s %s' % x for x in attribute_name)) 
f.write('\n'.join('%s %s %s %s %s %s %s' % x for x in attribute_type)) 
for i in range(len(student_list)): 
    f.write('\n'.join('%s %s %s %s %s %s %s' % x for x in student_list[i])) 
f.close() 

它给出错误说:

TypeError: not enough arguments for format string. 

任何人有,为什么它不工作的任何想法? 谢谢。

+0

错误在哪一行? – Hussain

+0

自此行发生错误f.write('\ n')。加入('%s%s%s%s%s%s%s'%x for attribute_name)) – MakaraPr

回答

3

更换这(以及其他类似事件):

'%s %s %s %s %s %s %s' % x for x in attribute_name) 

'%s %s %s %s %s %s %s' % tuple(x for x in attribute_name)) 

编辑:
其实你下面的代码看起来奇怪:

f.write('\n'.join('%s %s %s %s %s %s %s' % x for x in attribute_name)) 

的parame之三join已经是一个字符串,其结果实际上是将每个字符之间的换行符

>>> '\n'.join('1234') 
'1\n2\n3\n4' 

我猜你只需要这样:

f.write('%s %s %s %s %s %s %s\n' % tuple(x for x in attribute_name)) 

再次编辑:
@ John1024的回答看起来是正确的,你只需要直接使用列表名称就可以:

f.write('%s %s %s %s %s %s %s\n' % tuple(attribute_name)) # convert list to tuple 

再次编辑:
对不起,但我想我应该更明确地解释原因。
在使用Python格式化字符串时,参数列表,预计在一个元组经过:

'parameters: %s %s' %('param0', 'param1') 

虽然它的罚款以两种方式来写的时候,只有一个参数:

'parameters: %s' %('param0') 
'parameters: %s' %'param0' 

所以让我们来看看这个:

>>> lst = [1, 2] 
>>> '%d %d' % lst # this shall fail since the parameter list type doesn't match 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: %d format: a number is required, not list 
>>> '%d %d' %tuple(lst) # while this shall work 
'1 2' 
>>> tuple(lst) # this generates a tuple from lst 
(1, 2) 
+0

是的我想我的代码是这样但你的代码不起作用,f.write('%s %s%s%s%s%s%\ n'%attribute_name),我想你忘了放这样的元组, n'%tuple(attribute_name)),但是你知道为什么我们需要在attribute_name前面加上元组吗? – MakaraPr

+0

@MakaraPr对不起,我们已经修复了这个错误。原因是,我想我最好在答案中编辑它,而不是留下评论。请等一会儿。 – starrify

+0

你知道为什么我们需要在attribute_name前面的元组吗? – MakaraPr

1

如果你想创造的学生信息表,我猜测有一些变化,你会想对代码:

attribute_name = ("student_id", "name", "department", "sex", "age", "nationality",      "grade") 
attribute_type = ("INT", "CHAR(20)", "CHAR(50)", "CHAR(1)", "INT", "CHAR(3)", "FLOAT") 
student_list = [("student_id", "name", "department", "sex", "age", "nationality", "grade"), ("student_id", "name", "department", "sex", "age", "nationality", "grade")] 
f = open("student_information.txt", "w") 
f.write('%s %s %s %s %s %s %s\n' % attribute_name) 
f.write('%s %s %s %s %s %s %s\n' % attribute_type) 
for i in range(len(student_list)): 
    f.write('%s %s %s %s %s %s %s\n' % student_list[i]) 
f.close() 

这将产生一个文件,它看起来像:

student_id name department sex age nationality grade 
INT CHAR(20) CHAR(50) CHAR(1) INT CHAR(3) FLOAT 
student_id name department sex age nationality grade 
student_id name department sex age nationality grade 

(如果你想买的东西很好地排队,你应该指定宽度为%s格式项目。)

+0

... + 1。我在回答中给出了一个(更愚蠢的)解决方案。 – starrify