txtファイルからキーとキー値を削除する方法を教えてください。ユーザーがこのファイルからサラと呼ばれる人を削除したい場合例:入力が一致するCSVから行を削除する
Joe,Bloggs,[email protected],01269 512355, 1, 0, 0, 0,
Sarah,Brown,[email protected],01866 522555, 1, 0, 0, 0,
…
現在、私は次のようしている:
L = open("players.txt","r+")
delete_name1 = raw_input ("Enter the first name of the person you wish to delete: ")
for line in L:
s = line.strip()
string = s.split(",")
if delete_name1 == string[0]:
del string[:8] # Doesn't work
print delete_name1 + " has been deleted."
L.close() # Closes the file to free us usage space.
コードが受け入れられているが、それはtxtファイルを変更しません。更新された情報をtxtファイルに書き戻さなければならないような気がしますが、これを行う方法はわかりません。
Please select an option: 2
Enter the first name of the person you wish to delete: Sarah
Sarah has been deleted
この質問は広すぎます。あなたの現在の試みを提供し、あなたが直面した問題を記述してください。 –
ようこそ! http://stackoverflow.com/help/how-to-askをご覧ください。 テキストファイルのサンプルを書くこともできます。あなたはこれまで何をしていましたか、あなたは何を理解していませんか? – vmonteco
ファイル全体をいくつかのデータ構造に格納し、データ構造からSarahの情報を削除し、データ構造全体をファイルに書き戻します。 –