1

I want to overwrite the file name student_information.txt. I have tried :

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

It gives error saying :

TypeError: not enough arguments for format string.

Anyone have any ideas why it's not working? Thanks.

1
  • Error since this line f.write('\n'.join('%s %s %s %s %s %s %s' % x for x in attribute_name)) Commented Dec 8, 2013 at 7:02

2 Answers 2

3

Replace this (and also other similar occurrences):

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

with

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

EDITED:
Actually your following code looks weird:

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

the parameter to join is already a single string, and the result is actually inserting a newline character between each characters:

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

I guess you only need this:

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

EDITED AGAIN:
@John1024's answer looks the right way, you need only use the list name directly like:

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

YET EDITED AGAIN:
Sorry but I think I should explain the reasons more explicitly.
In Python when using a formatted string, the parameter list is expected to pass in a tuple:

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

While it's fine to write in both ways when there's only one parameter:

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

So let's look at this:

>>> 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)
Sign up to request clarification or add additional context in comments.

4 Comments

Yes I want my code to be like that but your code does not work,f.write('%s %s %s %s %s %s %s\n' % attribute_name), I think you forgot to put tuple like this one, f.write('%s %s %s %s %s %s %s\n' % tuple(attribute_name)) but do you know why we need tuple in front of attribute_name
@MakaraPr Sorry for that mistake, and Ray has already fixed it. The reason, I think I'd better edit it in the answer instead of leaving a comment. Please wait for a while.
do you know why we need tuple in front of attribute_name?
@MakaraPr Please see the last section of my newly edited answer.
1

If you are trying to create a table of student information, I am guessing that there are several changes you would want to make to the code:

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

This produces a file which looks like:

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

(If you want the items to line up nicely, you should specify widths to the %s format items.)

1 Comment

...+1. I was giving a (much more stupid) solution in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.