简体   繁体   中英

File not being read from first line(python)

I am trying to replace a specific index in a text file for the user that logs in (variable is username). The code works, but only for the last line in the text file. According to a previous similar question here on SO, my problem here is that I read the file twice, but shouldn't file.seek(0) set the cursor back to the beginning of this file? I have been stuck on this issue for days, any help would be appreciated. Thanks in advance. (And yes, I have tried all the answers listed in questions similar to mine)

username = "1211103899" #i put this line here for testing purposes
postcode1 = input("Please enter your current postcode: ")
file = open("userdetails.txt", "r")
userdetails = file.readlines()
    
for i in userdetails:
    i = i.strip().split('|') #this is to identify the separator (not very efficient way of doing it, i know)
file.seek(0)
file.close()
    
temp = open('temp', 'w')
with open('userdetails.txt', 'r') as f:
    for line in f:
        if username in line:
line = line.replace(i[5], postcode1)
    
        temp.write(line)
temp.close()
shutil.move('temp', 'userdetails.txt')

Here's an example of what's in the txt file:

Kumar|11/2|19|011256|Home|47500|Low Risk|1|1211104143|SJMC|23-01-2022|15:00
Muhammad|31/1|19|014576|Home|63000|High Risk|5|1211103899|Klinik Mediviron|17-01-2022|8:00
Vimal|12/5|21|012567|Office|47500|High Risk|5|1211104171|SJMC|26-01-2022|13:00

It's not clear why you think you need to read the file twice, or why you expect the seek to do anything useful at all immediately before closing the file.

The major problem is apparently that you hope i[5] should contain something which it doesn't. (It will end up containing the sixth field of the last line from the first time you loop over the file, and unnecessarily split and discard this value from all the other lines.) I'm guessing you want to replace the sixth field on the current line, but perhaps this is wrong; please edit your question to clarify what the code should do if so.

Probably try something like this instead.

import tempfile

username = "1211104171"
postcode1 = input("Please enter your current postcode: ")

with open("userdetails.txt", "r") as lines, tempfile.NamedTemporaryFile(delete=False) as temp:
    for line in lines:
        fields = line.split('|')
        if fields[2] == username:
            fields[4] = postcode1           
            line = '|'.join(fields)
        temp.write(line)
    shutil.move(temp.name, 'userdetails.txt')

The temporary file is probably not strictly necessary; maybe see also the fileinput module and its inline functionality.

Try this

username = "1211104171" #i put this line here for testing purposes
postcode1 = input("Please enter your current postcode: ")

file = open("userdetails.txt", "r")
userdetails = file.readlines()
file.close()

temp = open('temp', 'w')
for line in userdetails:
    if username in line:
        split = line.split('|')
        split[5] = postcode1
        l = '|'.join(split)
        temp.write(l)
        
temp.close()
shutil.move('temp', 'userdetails.txt')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM