N T
N T

Reputation: 53

My python code skips the first line of txt file?

So I'm working on this piece of code that copies lines from a .txt file to use them in an algorithm, the saves the results into another file. The program always skips the first line, and instead of outputting 6 lines, like in the first .txt file, it outputs 5. I've been at it for quite some time and I can't seem to find what is causing it. Here is the code.

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

Upvotes: 0

Views: 54

Answers (1)

DYZ
DYZ

Reputation: 57105

Your code does not follow many best practices, which makes it hard to comprehend and debug. Here's my take on how to make it more 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))

I am not sure what is encryptSave because you never defined that variable. I assume it is a string.

Upvotes: 1

Related Questions