替换文本文件中的一行
Replacing a line within a text file
基本上我想做的就是简单地覆盖包含分数的文本文件中的一行,虽然我在搜索了一段时间后没有任何运气
我对如何将包含旧列表的 'val' 替换为 'empty' 感到困惑,并且实质上是更新文件 'scores.txt' 以便替换行
index = 0
with open('scores.txt','r') as f:
x = f.readlines()
print(x)
name = input('Enter name: ')
print('\n')
c_1 = 0
c_2 = 0
empty = []
for val in x:
c_1 += 1
print("Iteration",c_1)
print('val',val)
if name in val:
print('True: ' + '"' + name + '"' , val)
empty.append(val)
empty = [i.split() for i in empty]
print(empty)
empty = [item for sublist in empty for item in sublist]
print(empty,'\n')
print('len empty',len(empty))
while len(empty) > 4:
del empty[1]
c_2 += 1
print("Del Iteration",c_2)
print('empty after del',empty)
break
elif name not in val:
print("False\n")
index+=1
这是里面可以看到的'scores.txt'
jill 9 10 7 8 5
bob 4 6 7
denas 2 4
john 1
我的目标是将 'jill' 的分数减少到只有 3,这是我的代码所做的,但是在退出代码并打开 'scores.txt'可见变化
我知道这个问题以前有人回答过,但我无法自己解决,因为我对 Python 还是新手:/
一般来说,您需要写入一个单独的文件,然后将新文件复制到旧文件上才能执行此操作。 (你可以使用fileinput or mmap貌似就地修改文件,但你可以自己研究这些)
举个例子,给定:
$ cat /tmp/scores.txt
jill 9 10 7 8 5
bob 4 6 7
denas 2 4
john 1
您可以这样修改文件:
with open('/tmp/scores.txt', 'r') as f, open('/tmp/mod_scores.txt', 'w') as fout:
for line in f:
if line.startswith('jill'):
line=' '.join(line.split()[0:4])
fout.write(line.strip()+"\n")
现在您有一个包含您想要的修改的文件:
$ cat /tmp/mod_scores.txt
jill 9 10 7
bob 4 6 7
denas 2 4
john 1
现在只需将新文件复制到旧文件之上即可。要复制,请使用 os.rename 现在可以将文件 scores.txt
修改到位。
根据评论,要获得最后 3 个分数与前 3 个分数,您可以这样做:
with open('/tmp/scores.txt', 'r') as f, open('/tmp/mod_scores.txt', 'w') as fout:
for line in f:
if line.startswith('jill'):
li=line.split()
line=' '.join([li[0]]+li[-3:])
fout.write(line.strip()+"\n")
为了以防万一(dwag 的回答是你应该使用的)这是你的代码在一些修复后工作:
index = 0
# empty and x must have global scope.
empty = []
x = []
with open('scores.txt','r') as f:
x = f.readlines()
name = input("Enter the name: ")
c_1 = 0
c_2 = 0
for val in x:
c_1 += 1
print("Iteration",c_1)
print('val',val)
if name in val:
empty.append(val)
empty = [i.split() for i in empty]
empty = [item for sublist in empty for item in sublist]
while len(empty) > 4:
del empty[1]
c_2 += 1
break
elif name not in val:
print("False")
index += 1
with open('scores.txt','w') as f:
x[0] = ' '.join(empty + ['\n'])
f.writelines(x)
基本上我想做的就是简单地覆盖包含分数的文本文件中的一行,虽然我在搜索了一段时间后没有任何运气
我对如何将包含旧列表的 'val' 替换为 'empty' 感到困惑,并且实质上是更新文件 'scores.txt' 以便替换行
index = 0
with open('scores.txt','r') as f:
x = f.readlines()
print(x)
name = input('Enter name: ')
print('\n')
c_1 = 0
c_2 = 0
empty = []
for val in x:
c_1 += 1
print("Iteration",c_1)
print('val',val)
if name in val:
print('True: ' + '"' + name + '"' , val)
empty.append(val)
empty = [i.split() for i in empty]
print(empty)
empty = [item for sublist in empty for item in sublist]
print(empty,'\n')
print('len empty',len(empty))
while len(empty) > 4:
del empty[1]
c_2 += 1
print("Del Iteration",c_2)
print('empty after del',empty)
break
elif name not in val:
print("False\n")
index+=1
这是里面可以看到的'scores.txt'
jill 9 10 7 8 5
bob 4 6 7
denas 2 4
john 1
我的目标是将 'jill' 的分数减少到只有 3,这是我的代码所做的,但是在退出代码并打开 'scores.txt'可见变化
我知道这个问题以前有人回答过,但我无法自己解决,因为我对 Python 还是新手:/
一般来说,您需要写入一个单独的文件,然后将新文件复制到旧文件上才能执行此操作。 (你可以使用fileinput or mmap貌似就地修改文件,但你可以自己研究这些)
举个例子,给定:
$ cat /tmp/scores.txt
jill 9 10 7 8 5
bob 4 6 7
denas 2 4
john 1
您可以这样修改文件:
with open('/tmp/scores.txt', 'r') as f, open('/tmp/mod_scores.txt', 'w') as fout:
for line in f:
if line.startswith('jill'):
line=' '.join(line.split()[0:4])
fout.write(line.strip()+"\n")
现在您有一个包含您想要的修改的文件:
$ cat /tmp/mod_scores.txt
jill 9 10 7
bob 4 6 7
denas 2 4
john 1
现在只需将新文件复制到旧文件之上即可。要复制,请使用 os.rename 现在可以将文件 scores.txt
修改到位。
根据评论,要获得最后 3 个分数与前 3 个分数,您可以这样做:
with open('/tmp/scores.txt', 'r') as f, open('/tmp/mod_scores.txt', 'w') as fout:
for line in f:
if line.startswith('jill'):
li=line.split()
line=' '.join([li[0]]+li[-3:])
fout.write(line.strip()+"\n")
为了以防万一(dwag 的回答是你应该使用的)这是你的代码在一些修复后工作:
index = 0
# empty and x must have global scope.
empty = []
x = []
with open('scores.txt','r') as f:
x = f.readlines()
name = input("Enter the name: ")
c_1 = 0
c_2 = 0
for val in x:
c_1 += 1
print("Iteration",c_1)
print('val',val)
if name in val:
empty.append(val)
empty = [i.split() for i in empty]
empty = [item for sublist in empty for item in sublist]
while len(empty) > 4:
del empty[1]
c_2 += 1
break
elif name not in val:
print("False")
index += 1
with open('scores.txt','w') as f:
x[0] = ' '.join(empty + ['\n'])
f.writelines(x)