我有這樣的代碼:爲什麼我的代碼打印這麼大的數字?
count = -1
with open("group.txt", "r") as f:
content = f.read()
with open("group2.txt", "r") as f:
mydict = eval(f.read())
print(content)
for x in range(0, len(mydict)):
count += 1
content = content.replace(str(mydict.keys()[count]), str(mydict.values()[count]))
with open("group3.txt", "w") as f:
f.write(content)
應全部更換mydict.keys()
與mydict.values()
。
當我運行它;它打印很長的數字。
這裏原來行:
<VertexRef> { 0 1 2 <Ref> { vpool } }
據打印了作爲該:
<VertexRef> { 81131131213851461468215053091131131401466142513121386133513091131131421505309113113140146614251312138613351296 81131131213851461468215053091131131401466142513121386133513091131131401466142771 811311312138514614682150530911311314014661425131213861335130911311394 <Ref> { vpool } }
它應該是:
<VertexRef> { 754 755 756 <Ref> { vpool } }
'mydict = eval(f.read())'每當你發現自己使用eval時,你就知道必須有更好的方法。在你的情況下,它是[pickle](https://docs.python.org/2/library/pickle.html),如果你必須將它寫入文件。 –