2012-04-03 55 views
0

我正在編寫一個簡單的程序,爲用戶提供一個帳戶。這包括用戶可以一遍又一遍地更改他或她的密碼。如果他或她包含信息作爲他們的電話號碼,則必須有使他們能夠更改自己的數據的選項。從Python中修改.txt

該程序將打開.txt文件並閱讀信息。

例如,這是信息的一小部分,該.txt包括:

 
123$333$Harold$321$Far far away3 
124$444$George$654$Far far away4 #I logged in with this account 
125$555$Louis$987$Far far away5 

現在假設喬治(誰登錄的一個)想改變他的ADRESS。我的意思是,一旦列表中的信息被「$」分隔,就很容易修改信息。這是修改後的名單將如何看起來像:

[124, 444, 'George', 654, '22 Acacia Avenue'] 

但問題是我怎麼能修改.txt,這樣的內容會是這樣?

 
123$333$Harold$321$Far far away3 
124$444$George$654$22 Acacia Avenue 
125$555$Louis$987$Far far away5 

我該如何做到這一點?

+3

使用數據庫,而不是文本文件。至少請參閱書架模塊。 – jordanm 2012-04-03 02:31:14

+2

另請參見:[jordanm指的] [shelv'模塊的文檔](http://docs.python.org/library/shelve.html)](http://stackoverflow.com/q/9986340#comment12761978_9986340 ) – icktoofay 2012-04-03 02:35:04

+1

另外,[Python有綁定](http://docs.python.org/library/sqlite3。html)轉換爲[SQLite3](http://www.sqlite.org/),它是一個小型可嵌入數據庫引擎。 – icktoofay 2012-04-03 02:37:46

回答

0

除非您使用的字段長度固定的文本文件,否則每次進行更改時都必須重寫整個文本文件(因爲您將在每個字段的起始位置之後更改編輯的字段) 。只要文件很小,您就不會特別注意到這一點,但如果文件超過幾十行,這將開始成爲一個重要的性能問題。

建議您使用數據庫的意見提供者在很大程度上是正確的,儘管這引入了一組關於使用什麼樣的數據庫的新的決定等,這可能是本作業旨在產生的問題類型後面的部分。 :-)

重寫文件每次都是微不足道的,但是浪費的(僞-ISH):

new_data = [124, 444, 'George', 654, '22 Acacia Avenue'] 

f = open(path, mode) # Open with some mode that makes sense for how you write it 
        # or clear the file before you write your new list 
lines = [] 

# Now we can iterate over every line in the file 
for line in f: 
    # Check to see if this line starts with the same data before the '$' as the 
    # first field of the new_data list 
    if line.split("$")[0] == new_data[0]: 
    # if it does, put in new_data instead of the line we just read 
    lines.append("$".join(new_data)) 
    else: 
    # Not the line we're looking for, write it back into the file so it stays the same 
    lines.append(line) 
f.writelines(lines) 
+0

感謝信息人! – stbamb 2012-04-03 05:22:05

0

,你可以讀取信息到元組的列表。然後用你想要的元素替換你想改變的元組。當您完成所有更改後,將列表重新寫入txt文件。

2

正如其他人在您對問題的評論中所建議的那樣,您可能想要使用非基於文件的解決方案 - 請查看所提供的良好鏈接。

如果你的功課,您需要使用一個文本文件(或要處理文件的學習是起見),你可以做這樣的事情:

inf.txt:

123$333$Harold$321$Far far away3 
124$444$George$654$Far far away4 
125$555$Louis$987$Far far away5 

mkt.py:

# Read the file 
with open('inf.txt', 'r') as f: 
    lines = f.readlines() 

# Split the lines by '$' 
lines_split = [line.split('$') for line in lines] 

# Change address of the second login 
line_to_change = lines_split[1] 
# Change just last column 
line_to_change[-1] = '22 Acacia Avenue\n' 

# Combine back 
lines_out = ['$'.join(line) for line in lines_split] 

# Write the file 
with open('outf.txt', 'w') as f: 
    f.writelines(lines_out) 

並執行它:

$ python mkt.py 
$ cat outf.txt 
123$333$Harold$321$Far far away3 
124$444$George$654$22 Acacia Avenue 
125$555$Louis$987$Far far away5 
$ 
+0

我不認爲它很好,直接給他們答案,但我很高興你至少打破了評論,而不是粘貼一段代碼。 – jdi 2012-04-03 02:45:49

+0

@jdi謝謝 - 是的,試圖在教育意義上有所幫助,希望它的工作:) – 2012-04-03 02:48:41

+0

非常感謝@ icyrock.com 現在我有我的修改文件,與新的信息! – stbamb 2012-04-03 05:21:04