无法修复python中的混合操作数

无法修复python中的混合操作数,python,python-3.x,Python,Python 3.x,我正在创建一个压缩文本(包括普通字母和标点符号等)的程序。但是,我遇到了某种混合操作数类型的错误,我不知道如何修复。我试过阅读其他关于这个主题的帖子,但我不明白它是如何工作的,以及如何将其应用到我的代码中 print("The compression program has started") myDict={} revDict={} sentList=[] posList=[] num = 0 sentence = open("pyCompress.txt","r

我正在创建一个压缩文本(包括普通字母和标点符号等)的程序。但是,我遇到了某种混合操作数类型的错误,我不知道如何修复。我试过阅读其他关于这个主题的帖子,但我不明白它是如何工作的,以及如何将其应用到我的代码中

  print("The compression program has started")

  myDict={}
  revDict={}
  sentList=[]
  posList=[]
  num = 0
  sentence = open("pyCompress.txt","r").read().split()


 for word in sentence:
      if word not in myDict:
      myDict[word] = num
      num += 1

 print(sentence)
 print(myDict)

 for k, v in myDict.items():
       revDict[v] = k

 file = open("Positions.txt","w")
 for word in sentence:
            file.write((myDict[word]) + " ")
            file.close()
除此之外还有更多的代码


我得到的错误是:TypeError:不支持的+操作数类型:'int'和'str'

myDict[word]
强制转换为
str
,这样您就可以执行串联,以及移动
文件。如其他人所述,将
关闭到循环外部。因此,只有像这样更改最后一个循环才能修复您所遇到的错误:

for word in sentence:
    file.write((str(myDict[word]) + " "))
file.close()

Python不允许向字符串添加整数:

>>> 1 + "a"
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    1 + "a"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
或者使用
%
符号或
{}
符号将其格式化:

>>> "the number %d is prime"%874193
'the number 874193 is prime'
>>> "please give me {n} apples".format(n=2)
'please give me 2 apples'
您还可以使用
print
为您处理转换:

print(myDict[word], file = file, end=" ")
接下来,您需要确保在写入所有需要写入的数据后关闭文件,在您的情况下,这将在for循环之后:

for word in sentence:
    print(myDict[word], file = file, end=" ")
file.close() #un-indent
尽管您可能希望使用
with
语句来确保正确处理文件:

with open("Positions.txt","w") as file:
    for word in sentence:
        print(myDict[word], file = file, end=" ")
assert file.closed #the file is closed at the end of with block

试试这个,阅读我的评论,了解我所做的更改

print("The compression program has started")

myDict={}
revDict={}
sentList=[]
posList=[]
num = 0
sentence = open("pyCompress.txt","r").read().split()

for word in sentence:
    if word not in myDict:
        # needs to convert to str
        # also indentation problem here
        myDict[word] = str(num)
        num += 1

print(sentence)
print(myDict)

for k, v in myDict.items():
    revDict[v] = k

file = open("Positions.txt","w")
for word in sentence:
    file.write((myDict[word]) + " ")
# indentation problem here
file.close()
如果要将键和值都写入文件,请尝试以下操作:


file.write(word+”:“+myDict[word]+”)

请发布错误消息好吗?TypeError:不支持的操作数类型为+:'int'和'str'
myDict[word]+”
dict的值是
int
,因此在添加它之前需要将其转换为
str
str
这是因为您在每次写行后都要关闭文件,但您只在第一次打开它。请在循环后关闭文件。不在!或者使用.readlines()将文件的数据保存到列表中,然后循环执行该操作!
print("The compression program has started")

myDict={}
revDict={}
sentList=[]
posList=[]
num = 0
sentence = open("pyCompress.txt","r").read().split()

for word in sentence:
    if word not in myDict:
        # needs to convert to str
        # also indentation problem here
        myDict[word] = str(num)
        num += 1

print(sentence)
print(myDict)

for k, v in myDict.items():
    revDict[v] = k

file = open("Positions.txt","w")
for word in sentence:
    file.write((myDict[word]) + " ")
# indentation problem here
file.close()