删除 Python 中的文件行
Deleting File Lines in Python
我正在尝试创建一个接受用户名和高分的程序,如果他们已经是用户,他们会更新到他们的新高分,或者如果不是,则只添加高分。
我的代码是:
try:
a = open("data", "r+")
except FileNotFoundError:
a = open("data", "w")
a = open("data", "r+")
b = a.read()
user = input("Username: ")
user2 = list(user)
if user in b:
old = input("What is your old highscore? ")
new = input("What is your new highscore? ")
b2 = b.split()
for line in b2:
#Where I want to edit.
line=line.replace(old, new)
print(line)
else:
new = input("What is your highscore? ")
a.write(user + " " + new + "\n")
a.close()
有谁知道如何将文件中的旧文件替换为新文件?
先关,后
b = a.read()
写
a.close()
a = open("data","w")
看看它会带你去哪里。
简单的回答是:这是不可能的。操作系统及其文件操作没有 "lines" 的概念。它们处理二进制数据块。一些库,例如 Python 的标准库,在上面为 reading 行提供了一个方便的抽象 - 但它们不允许您处理单独的行。
那么如何解决问题呢?只需打开文件,读取所有行,就地处理有问题的行,然后再次写出 整个 文件。
import tempfile
highscore_file = tempfile.mktemp()
with open(highscore_file, "w") as outf:
outf.write("peter 1000\nsarah 500\n")
player = "sarah"
score = 2000
output_lines = []
with open(highscore_file) as inf:
for line in inf:
if player in line:
# replace old with new line. Don't forget trailing newline!
line = "%s %i\n" % (player, score)
output_lines.append(line)
with open(highscore_file, "w") as outf:
outf.write("".join(output_lines))
with open(highscore_file) as inf:
print inf.read()
我建议您改用一些标准格式来保存信息,例如 JSON、YAML、XML、CSV、pickle 或其他格式。然后你需要的是读取文件并将其解析为本机数据结构(在这种情况下可能是dict
),修改它(这是微不足道的),然后将其写回。
带有 json
的示例(人类可读,非常易于使用):
import json
# loading data
try:
with open("data") as a:
b = json.load(a) # b is dict
except FileNotFoundError:
b = {}
# user
name = input("What's your name? ")
score = int(input("What's your high score? "))
# manipulating data
b[name] = score
# writing back
with open("data", "w") as a:
json.dump(b, a)
带有 shelve
的示例(不是人类可读的,但非常易于使用):
import shelve
name = input("What's your name? ")
score = int(input("What's your high score? "))
with shelve.open("bin-data") as b:
b[name] = score # b is dict-like
我正在尝试创建一个接受用户名和高分的程序,如果他们已经是用户,他们会更新到他们的新高分,或者如果不是,则只添加高分。
我的代码是:
try:
a = open("data", "r+")
except FileNotFoundError:
a = open("data", "w")
a = open("data", "r+")
b = a.read()
user = input("Username: ")
user2 = list(user)
if user in b:
old = input("What is your old highscore? ")
new = input("What is your new highscore? ")
b2 = b.split()
for line in b2:
#Where I want to edit.
line=line.replace(old, new)
print(line)
else:
new = input("What is your highscore? ")
a.write(user + " " + new + "\n")
a.close()
有谁知道如何将文件中的旧文件替换为新文件?
先关,后
b = a.read()
写
a.close()
a = open("data","w")
看看它会带你去哪里。
简单的回答是:这是不可能的。操作系统及其文件操作没有 "lines" 的概念。它们处理二进制数据块。一些库,例如 Python 的标准库,在上面为 reading 行提供了一个方便的抽象 - 但它们不允许您处理单独的行。
那么如何解决问题呢?只需打开文件,读取所有行,就地处理有问题的行,然后再次写出 整个 文件。
import tempfile
highscore_file = tempfile.mktemp()
with open(highscore_file, "w") as outf:
outf.write("peter 1000\nsarah 500\n")
player = "sarah"
score = 2000
output_lines = []
with open(highscore_file) as inf:
for line in inf:
if player in line:
# replace old with new line. Don't forget trailing newline!
line = "%s %i\n" % (player, score)
output_lines.append(line)
with open(highscore_file, "w") as outf:
outf.write("".join(output_lines))
with open(highscore_file) as inf:
print inf.read()
我建议您改用一些标准格式来保存信息,例如 JSON、YAML、XML、CSV、pickle 或其他格式。然后你需要的是读取文件并将其解析为本机数据结构(在这种情况下可能是dict
),修改它(这是微不足道的),然后将其写回。
带有 json
的示例(人类可读,非常易于使用):
import json
# loading data
try:
with open("data") as a:
b = json.load(a) # b is dict
except FileNotFoundError:
b = {}
# user
name = input("What's your name? ")
score = int(input("What's your high score? "))
# manipulating data
b[name] = score
# writing back
with open("data", "w") as a:
json.dump(b, a)
带有 shelve
的示例(不是人类可读的,但非常易于使用):
import shelve
name = input("What's your name? ")
score = int(input("What's your high score? "))
with shelve.open("bin-data") as b:
b[name] = score # b is dict-like