I am having speed issues using multiple "if/elif statements" when trying to read through and print a large file. When timing my code it took almost 10 mins to read through and replace 2MB file lol. The sample file I used had roughly 8 characters on a line. Example (ABAABAA, AAAAA, BAABABAB)
with open("text2.txt","r") as File1:
for line in File1:
W = 'A'
Z = 'B'
counting1 = line.count(W)
counting2 = line.count(Z)
if counting1 == 1:
removeW = line.replace(W,'C')
removeZ = line.replace(Z,'D')
print (removeW)
print (removeZ)
if counting2 == 2:
removeW = line.replace(W,'E')
removeZ = line.replace(Z,'F')
print (removeW)
print (removeZ)
elif counting1 == 2:
removeW = line.replace(W,'G')
removeZ = line.replace(Z,'H')
if counting2 == 3:
removeW = line.replace(W,'E')
removeZ = line.replace(Z,'F')
print (removeW)
print (removeZ)
elif counting1 == 3:
removeW = line.replace(W,'I')
removeZ = line.replace(Z,'J')
print (removeW)
print (removeZ)
I have a decent computer, because I'm so new at coding can someone assist me with cleaning that up and/or teach me just a better process for speed purposes. I have 2 nested if statements and because of the different possibilities not sure a better way.
Aucun commentaire:
Enregistrer un commentaire