MakaraPr
MakaraPr

Reputation: 171

Writing file in Python

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.

Upvotes: 1

Views: 96

Answers (2)

starrify
starrify

Reputation: 14731

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)

Upvotes: 3

John1024
John1024

Reputation: 113814

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

Upvotes: 1

Related Questions