在python中操作文件内数据的代码

在python中操作文件内数据的代码,python,Python,文本文件中的数据: GOPU 433 33332.000000 GOPAL 20 22233.000000 RAMU 33 76532.000000 def modify(): another='Y' while another=='Y': print "\nEnter name of employee to modify" empname=raw_input() import os fp.seek(0,os.SE

文本文件中的数据:

GOPU 433 33332.000000
GOPAL 20 22233.000000
RAMU 33 76532.000000
def modify():
    another='Y'
    while another=='Y':
        print "\nEnter name of employee to modify"
        empname=raw_input()
        import os
        fp.seek(0,os.SEEK_SET)
        for line in fp:
            if len(line)>1:                         #To handle line feed
                e.name, e.age, e.bs=line.split()
                e.age = int(e.age)                  # convert age from string to int
                e.bs = float(e.bs)
                if(cmp(e.name,empname)==0):
                    c=len(line)
                    print "\nEnter new name,age & bs"
                    e.name=raw_input()
                    e.age=eval(raw_input())
                    e.bs=eval(raw_input())
                    import os
                    fp.seek(-c,os.SEEK_CUR)
                    ch="%s %d %f" % (e.name,e.age,e.bs)
                    fp.writelines("\n")
                    fp.writelines(ch)
                    fp.writelines("\n")
                    break
        print "\nModify another Record(Y/N)"
        another=raw_input()
下面是修改文本文件内数据的代码:

GOPU 433 33332.000000
GOPAL 20 22233.000000
RAMU 33 76532.000000
def modify():
    another='Y'
    while another=='Y':
        print "\nEnter name of employee to modify"
        empname=raw_input()
        import os
        fp.seek(0,os.SEEK_SET)
        for line in fp:
            if len(line)>1:                         #To handle line feed
                e.name, e.age, e.bs=line.split()
                e.age = int(e.age)                  # convert age from string to int
                e.bs = float(e.bs)
                if(cmp(e.name,empname)==0):
                    c=len(line)
                    print "\nEnter new name,age & bs"
                    e.name=raw_input()
                    e.age=eval(raw_input())
                    e.bs=eval(raw_input())
                    import os
                    fp.seek(-c,os.SEEK_CUR)
                    ch="%s %d %f" % (e.name,e.age,e.bs)
                    fp.writelines("\n")
                    fp.writelines(ch)
                    fp.writelines("\n")
                    break
        print "\nModify another Record(Y/N)"
        another=raw_input()
输出:

Enter name of employee to modify: GOPAL
Enter new name,age & bs: PANDEY
24
38374
文件内容变为:

GOPU 433 33332.000000
GOPAL 20 22233.000000
PANDEY 24 38374
当我试图修改
GOPAL
的数据时,它正在修改下一个员工的数据,即<代码>RAMU

不知道为什么会这样


请为我提供解决方案?

您作为迭代器在文件对象上循环:

for line in fp:
它使用一个内部预读缓冲区;这意味着使用
f.seek()
相对于缓冲区读取的位置而不是当前正在处理的行工作

您必须使用
f.readline()
调用:

for line in iter(fp.readline, ''):
在没有预读缓冲区的情况下,将提供相同的循环效果

然而,您的方法将遇到其他问题;行大小的任何更改都不会覆盖足够的行或覆盖下一行。如果写入的输出与要替换的行长度不完全匹配,则文件不会自动收缩或增长

相反,您必须将整个文件重写为临时文件并将其移回原位

进一步评论:

  • 使用
    fp.write()
    fp.writelines()
    将分别写入每个字符。它会起作用的,但只是偶然和缓慢的

  • 比较字符串时不要使用
    cmp()
    。只需使用
    string1==string2

  • 不要在
    int()
    float()
    可以使用的地方使用
    eval()

  • 您只需要在顶部导入模块一次。那些额外的
    导入操作系统
    行是多余的


当一个简单的
e.name==empname
就可以了时,为什么要使用
cmp()
?而您通常会使用
int()
float()
来转换输入值;不是
eval()
。他正在向文件写入,您从原始输入()默认输入的字符串应该可以吧?@yopy:The
raw\u input()
仍然来自用户;当您只需要整数或浮点值时,使用
eval()
很少是正确的选择。修改文本文件的片段总是一个坏主意。最好将其全部重写(请参见其答案中的@MartijnPieters)注释)现在,它正在更改所需员工的数据,但当编辑的员工姓名过长时,它将覆盖下一个员工姓名的某些部分…:(@user3223301这就是你不应该这样修改文本文件的原因…Martijn在他的回答中告诉你这个问题。你能给我一些使用临时文件修改数据的提示吗?主要的是,我不想通过修改来更改序列,也就是说,它应该在其位置修改数据,而不是删除和写入数据a位于尾端,请参见示例。