如何在python中将元组写入文本文件?

时间:2017-11-26 23:09:58

标签: python

我正在尝试将ISBN-10转换为ISBN-13转换为文本文档,但每次尝试都会出现此错误。

  

第244行,在conv10to13FILE outfile.write(ISBN13)   TypeError:必须是str,而不是元组

以下是代码:

filename = input("Enter the name of the .txt file: ")
getoutput = input("Name of text file where results will be placed: ")
outfile = open(getoutput, "a")

for line in open(filename, "r"):
    if "-" in line:
        line = line.replace("-", "")
    D1 =int(line[0])*3
    D2 =int(line[1])*1
    D3 =int(line[2])*3
    D4 =int(line[3])*1
    D5 =int(line[4])*3
    D6 =int(line[5])*1
    D7 =int(line[6])*3
    D8 =int(line[7])*1
    D9 =int(line[8])*3            
    Sum=(9+21+8+D1+D2+D3+D4+D5+D6+D7+D8+D9)
    Mod= Sum % 10
    D11= 11 - Mod - 1
    if D11 == 10:
        D11 = 'X'


    ISBN13=(978,"-",line[0],"-",line[1],line[2],"-",line[3],line[4],line[5],line[6],line[7],line[8],"-",D11)


    outfile.write(ISBN13)
    tomenu = input("Press Enter to return to Menu")

1 个答案:

答案 0 :(得分:1)

我相信你只想加入元组中的所有元素:

outfile.write(''.join(str(item) for item in ISBN13))