python - 它如何将列表中的结果保存到 .txt 文件中?
问题描述
在此代码中,脚本从“Base.txt”文件中获取所有坐标(x, y),但不幸的是,仅从第 1 行将结果保存到“Save.txt”文件中,并跳过了坐标线的其余部分列表中的值。
from ecpy.curves
import Curve,Point
with open("Base.txt", "rt") as base:
for line in base.read().splitlines():
x, y = map(lambda v: int(v, 16), line[1: -1].split(" , "))
cv = Curve.get_curve('secp256k1')
A = Point(x,y,cv)
C = 6
B = A*C
with open("Save.txt", "w") as file:
print(B,file=file, sep="\n")
它如何将列表中的所有结果保存到“Save.txt”文件中?
解决方案
for 循环
with open("Base.txt", "rt") as base:
for line in base.read().splitlines():
x, y = map(lambda v: int(v, 16), line[1: -1].split(" , "))
覆盖每一行x
。y
您应该在 for 循环中包含所有内容,并以“附加”模式打开文件,并在文件上使用该write
函数。
from ecpy.curves
import Curve,Point
with open("Base.txt", "rt") as base:
for line in base.read().splitlines():
x, y = map(lambda v: int(v, 16), line[1: -1].split(" , "))
cv = Curve.get_curve('secp256k1')
A = Point(x,y,cv)
C = 6
B = A*C
with open("Save.txt", "a") as file:
file.write(str(B)) # should implement the __str__ or __repr__ method
file.write("\n")
推荐阅读
- django - 使用 Django,我如何将文件作为变量加载,使其成为全局变量并使用文件中的文本/数据
- python - Small World 聚类系数和特征路径长度
- c# - 程序在调试器中逐步正确运行,但不正常
- pandas - 结合 groupby 和 resample
- reactjs - 用 axios 取消突变 react-apollo
- php - 尝试在 linux powershell 上导入模块 ActiveDirectory
- r - 如何根据一个变量逐行删除 kable() 表中的灰色突出显示和不同的行?
- mongodb - 无法模拟 mongoRepository 实例
- c# - 我可以从我的库中访问 void 到正在使用它的应用程序吗?
- azure-data-factory - 如何创建动态管道以将数据从多个本地 SQL 服务器复制到单个服务器