我的 python 代码跳过了 txt 文件的第一行?
My python code skips the first line of txt file?
所以我正在处理这段代码,它从 .txt 文件中复制行以在算法中使用它们,然后将结果保存到另一个文件中。该程序总是跳过第一行,而不是像在第一个 .txt 文件中那样输出 6 行,而是输出 5 行。我已经研究了很长时间,但似乎无法找到导致它的原因。这是代码。
encryptString = []
counter = 0
dTagID = []
longVer = []
shortVer = []
#reading
fRead = open("certTags.txt", "r")
for line in fRead.readlines():
dTagID.append(line)
lengthList = len(dTagID)
while counter < lengthList:
tempSave = encryptSave + dTagID[counter]
tempSave = int(tempSave)
# Encryption
msg = tempSave
msge = msg**e
msgn = msge % N
msgn = str(msgn)
longVer.append(msgn)
# Checksum
checksum = sum(map(int, msgn))
checksum = str(checksum)
shortVer.append(encryptSave+checksum)
# Saving to file
fWriteS = open("shortVer.txt", "a+")
fWriteS.write(shortVer[counter] + '\n')
fWriteL = open("longVer.txt", "a+")
fWriteL.write(longVer[counter] + '\n')
counter = counter + 1
您的代码没有遵循很多最佳实践,这使得理解和调试变得困难。这是我对如何让它更像 pythonic 的看法:
longVer = []
shortVer = []
#reading
with open("certTags.txt", "r") as fRead:
for line in fRead:
msg = int(encryptSave + line)
# Encryption
msg = str(msg**e % N)
longVer.append(msg)
# Checksum
checksum = str(sum(map(int, msg)))
shortVer.append(encryptSave + checksum)
# Saving to file
with open("shortVer.txt", "w") as fWriteS:
fWriteS.write("\n".join(shortVer))
with open("longVer.txt", "w") as fWriteL:
fWriteL.write("\n".join(longVer))
我不确定 encryptSave
是什么,因为您从未定义过该变量。我假设它是一个字符串。
所以我正在处理这段代码,它从 .txt 文件中复制行以在算法中使用它们,然后将结果保存到另一个文件中。该程序总是跳过第一行,而不是像在第一个 .txt 文件中那样输出 6 行,而是输出 5 行。我已经研究了很长时间,但似乎无法找到导致它的原因。这是代码。
encryptString = []
counter = 0
dTagID = []
longVer = []
shortVer = []
#reading
fRead = open("certTags.txt", "r")
for line in fRead.readlines():
dTagID.append(line)
lengthList = len(dTagID)
while counter < lengthList:
tempSave = encryptSave + dTagID[counter]
tempSave = int(tempSave)
# Encryption
msg = tempSave
msge = msg**e
msgn = msge % N
msgn = str(msgn)
longVer.append(msgn)
# Checksum
checksum = sum(map(int, msgn))
checksum = str(checksum)
shortVer.append(encryptSave+checksum)
# Saving to file
fWriteS = open("shortVer.txt", "a+")
fWriteS.write(shortVer[counter] + '\n')
fWriteL = open("longVer.txt", "a+")
fWriteL.write(longVer[counter] + '\n')
counter = counter + 1
您的代码没有遵循很多最佳实践,这使得理解和调试变得困难。这是我对如何让它更像 pythonic 的看法:
longVer = []
shortVer = []
#reading
with open("certTags.txt", "r") as fRead:
for line in fRead:
msg = int(encryptSave + line)
# Encryption
msg = str(msg**e % N)
longVer.append(msg)
# Checksum
checksum = str(sum(map(int, msg)))
shortVer.append(encryptSave + checksum)
# Saving to file
with open("shortVer.txt", "w") as fWriteS:
fWriteS.write("\n".join(shortVer))
with open("longVer.txt", "w") as fWriteL:
fWriteL.write("\n".join(longVer))
我不确定 encryptSave
是什么,因为您从未定义过该变量。我假设它是一个字符串。