write()在Python中不包含关键字参数

时间:2019-10-15 18:17:40

标签: python-3.7.4

我收到此错误:

TypeError: write() takes no argument

我试图写为str(k),但没有帮助

f = open("lab8_674046492_answers.txt","w")
for k,v in sorted(course_catalog.items()):
    f.write(k,end='')
    f.write(':',v)
f.close()
f = open("lab8_674046492_answers.txt","r")
print(f.read())

我期望列表的键和值。我该如何解决?

2 个答案:

答案 0 :(得分:0)

f = open("lab8_674046492_answers.txt","w")
for k,v in sorted(course_catalog.items()):
    f.write(k,'')//why use "end="
    f.write(':',v)
f.close()
f = open("lab8_674046492_answers.txt","r")
print(f.read())

在函数中无需使用"end=''",仅需使用f.write(k,'') 或者,如果您尝试实际编写end='',请确保将其用引号引起来:

f = open("lab8_674046492_answers.txt","w")
for k,v in sorted(course_catalog.items()):
    f.write(k, "end=''")//Make sure to put it in quotes
    f.write(':',v)
f.close()
f = open("lab8_674046492_answers.txt","r")
print(f.read())

答案 1 :(得分:0)

此错误是由于在函数中使用了end =

看到这个... Getting error: write() takes no keyword arguments