File = input("Please enter the name for your txt. file: ")
fileName = (File + ".txt")
WRITE = "w"
APPEND = "a"
file = []
name = " "
while name != "DONE" :
name = input("Please enter the guest name (Enter DONE if there is no more names) : ").upper()
fileName.append(name)
fileName.remove("DONE")
print("The guests list in alphabetical order, and it will save in " + fileName + " :")
file.sort()
for U in file :
print(U)
file = open(fileName, mode = WRITE)
file.write(name)
file.close()
print("file written successfully.")
我只是练习用Python编写文件,但发生了一些不好的事情。
以下是关于此的一些错误:
fileName.remove("DONE")
仍在展示' str'错误。
答案 0 :(得分:1)
Python字符串是不可变的。因此,您无法在其上使用in
。请改用append()
:
+=
这是
的简写fileName += name
注意字符串中没有附加任何内容,而是创建一个新字符串,然后分配给fileName = fileName + name
。
答案 1 :(得分:1)
filename=filename+name
使用上面的代码
答案 2 :(得分:0)
试试这个。 我以为你在变量名中有些错误。
Vector<Vector<Integer>> uSupVec = new Vector<Vector<Integer>>();
Vector<Integer> uSup = new Vector<Integer>();
Boolean isRep = false;
for (int i = 1; i <numSup ; i++) {
uSup.clear();
for (int l = 0; l < i; l++) {
if (supName[i] == supName[l]) {
isRep = true;
}}
if (!isRep){uSup.add(i);}
for (int j = i+1; j < numSup; j++) {
if (supName[i] == supName[j]) {
if (!isRep){uSup.add(j);}
}}
isRep = false;
uSupVec.add((Vector) uSup);
}
答案 3 :(得分:0)
刚刚蝙蝠,你不能附加到一个人身上。
fileName.append(name) #how can you append or remove anything into or from this when it contains toople?
另一件事,我不知道你正在使用什么版本的python,但我从未见过像这样的表达
file = open(fileName, mode = WRITE) #this should be something like (file=open(fileName,"w"))
只需全面检查您的代码。就像我说你不能添加或删除东西的东西;只在列表和词典中。
答案 4 :(得分:0)
append
是list's method,其中代码中声明的fileName被视为字符串。如果您打算将字符串附加到文件,请以“追加”模式打开文件并写入:
with open(aFile + ".txt", "a") as f:
f.write("appended text")