将str转换为int python

2024-09-28 21:08:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个读文件转换一个str,但它似乎不起作用,我必须用这个int从另一个文件中取出。。。在

我试过int(variable),但没用。。在

写在我的手机上,很抱歉有任何错误

我将这个int从start保存到一个.txt,然后重新打开并再次读取/写入(save file)

def save():
hp = 20
hp1 = str(hp)
a1 = open("saves\DQ1", "w")
a1.write(hp1)

def load():
a1 = open("saves\DQ1", "r")
hp = a1.read()

^{pr2}$

一。在


Tags: 文件savedefa1错误openvariablestart
1条回答
网友
1楼 · 发布于 2024-09-28 21:08:55

既然我对这个问题不太了解,而你却在用手机发帖,我先从一个简单的解决方案开始。看看这对你是否有用:

answer = []
with open('path/to/input') as infile:
  for line in infile:
    answer.extend([int(i) for i in line.strip().split()])

相关问题 更多 >