Hướng dẫn how do you write in a specific line in a file in python? - làm thế nào để bạn viết trong một dòng cụ thể trong một tệp trong python?

Nếu văn bản của bạn chỉ chứa một cá nhân:

import re

# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 ']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def roplo[file_name,what]:
    patR = re.compile['^[[^\r\n]+[\r\n]+][^\r\n]+']
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        f.seek[0]
        f.write[patR.sub['\\1'+what,ch]]
roplo['pers.txt','Mage']


# after treatment
with open['pers.txt','rb'] as h:
    print '\nexact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]

Nếu văn bản của bạn chứa một số cá nhân:

Nhập RE

# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]

Nếu công việc của người Viking của một cá nhân có chiều dài không đổi ở Texte, bạn chỉ có thể thay đổi phần của Texte tương ứng với công việc của người Hồi giáo, cá nhân mong muốn: đó là ý tưởng tương tự như người gửi.

Nhưng theo tôi, tốt hơn là đặt các đặc điểm của các cá nhân vào một từ điển được ghi lại trong hồ sơ với cpickle:

from cPickle import dump, load

with open['cards','wb'] as f:
    dump[{'Dan':['Warrior',500,1,0],'Jim':['dragonfly',300,2,10],'Somo':['cosmonaut',490,3,65]},f]

with open['cards','rb'] as g:
    id_cards = load[g]
print 'id_cards before change==',id_cards

id_cards['Jim'][0] = 'Wizard'

with open['cards','w'] as h:
    dump[id_cards,h]

with open['cards'] as e:
    id_cards = load[e]
print '\nid_cards after change==',id_cards

Trong bài viết này, chúng tôi sẽ viết một chương trình Python để thay thế các dòng cụ thể trong tệp.

Trước tiên chúng tôi sẽ mở tệp ở chế độ chỉ đọc và đọc tất cả các dòng bằng cách sử dụng readlines [], tạo danh sách các dòng lưu trữ nó trong một biến. Chúng tôi sẽ thực hiện các thay đổi cần thiết cho một dòng cụ thể và sau đó, chúng tôi mở tệp ở chế độ chỉ ghi và viết dữ liệu đã sửa đổi bằng WriteLines [].readlines[], creating a list of lines storing it in a variable. We will make the necessary changes to a specific line and after that, we open the file in write-only mode and write the modified data using writelines[].

Tệp để trình diễn:

Explanation:

Đầu tiên, hãy mở tệp ở chế độ chỉ đọc và đọc từng dòng tệp bằng phương thức readlines [] và lưu trữ nó trong một biến. readlines[] method, and store it in a variable.

with open['example.txt','r',encoding='utf-8'] as file:
    data = file.readlines[]

Biến sẽ chứa một danh sách các dòng, in nó sẽ hiển thị tất cả các dòng có bên trong danh sách.

print[data]

Thực hiện các thay đổi cần thiết cho một dòng cụ thể.[Ở đây, tôi đã sửa đổi dòng thứ hai]

data[1] = "Here is my modified Line 2\n"

Mở lại tệp ở chế độ chỉ ghi và ghi dữ liệu đã sửa đổi bằng phương thức writeLines [].writelines[] method.

With open['example.txt', 'w', encoding='utf-8'] as file:
    file.writelines[data]

Dưới đây là việc thực hiện:

Python3

with open

# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
0
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
1
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
2
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
3
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
4
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
5
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
6
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
7
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
8
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
9

from cPickle import dump, load

with open['cards','wb'] as f:
    dump[{'Dan':['Warrior',500,1,0],'Jim':['dragonfly',300,2,10],'Somo':['cosmonaut',490,3,65]},f]

with open['cards','rb'] as g:
    id_cards = load[g]
print 'id_cards before change==',id_cards

id_cards['Jim'][0] = 'Wizard'

with open['cards','w'] as h:
    dump[id_cards,h]

with open['cards'] as e:
    id_cards = load[e]
print '\nid_cards after change==',id_cards
0
from cPickle import dump, load

with open['cards','wb'] as f:
    dump[{'Dan':['Warrior',500,1,0],'Jim':['dragonfly',300,2,10],'Somo':['cosmonaut',490,3,65]},f]

with open['cards','rb'] as g:
    id_cards = load[g]
print 'id_cards before change==',id_cards

id_cards['Jim'][0] = 'Wizard'

with open['cards','w'] as h:
    dump[id_cards,h]

with open['cards'] as e:
    id_cards = load[e]
print '\nid_cards after change==',id_cards
1
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
5
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
8
from cPickle import dump, load

with open['cards','wb'] as f:
    dump[{'Dan':['Warrior',500,1,0],'Jim':['dragonfly',300,2,10],'Somo':['cosmonaut',490,3,65]},f]

with open['cards','rb'] as g:
    id_cards = load[g]
print 'id_cards before change==',id_cards

id_cards['Jim'][0] = 'Wizard'

with open['cards','w'] as h:
    dump[id_cards,h]

with open['cards'] as e:
    id_cards = load[e]
print '\nid_cards after change==',id_cards
4

from cPickle import dump, load

with open['cards','wb'] as f:
    dump[{'Dan':['Warrior',500,1,0],'Jim':['dragonfly',300,2,10],'Somo':['cosmonaut',490,3,65]},f]

with open['cards','rb'] as g:
    id_cards = load[g]
print 'id_cards before change==',id_cards

id_cards['Jim'][0] = 'Wizard'

with open['cards','w'] as h:
    dump[id_cards,h]

with open['cards'] as e:
    id_cards = load[e]
print '\nid_cards after change==',id_cards
5
from cPickle import dump, load

with open['cards','wb'] as f:
    dump[{'Dan':['Warrior',500,1,0],'Jim':['dragonfly',300,2,10],'Somo':['cosmonaut',490,3,65]},f]

with open['cards','rb'] as g:
    id_cards = load[g]
print 'id_cards before change==',id_cards

id_cards['Jim'][0] = 'Wizard'

with open['cards','w'] as h:
    dump[id_cards,h]

with open['cards'] as e:
    id_cards = load[e]
print '\nid_cards after change==',id_cards
6

from cPickle import dump, load

with open['cards','wb'] as f:
    dump[{'Dan':['Warrior',500,1,0],'Jim':['dragonfly',300,2,10],'Somo':['cosmonaut',490,3,65]},f]

with open['cards','rb'] as g:
    id_cards = load[g]
print 'id_cards before change==',id_cards

id_cards['Jim'][0] = 'Wizard'

with open['cards','w'] as h:
    dump[id_cards,h]

with open['cards'] as e:
    id_cards = load[e]
print '\nid_cards after change==',id_cards
7
from cPickle import dump, load

with open['cards','wb'] as f:
    dump[{'Dan':['Warrior',500,1,0],'Jim':['dragonfly',300,2,10],'Somo':['cosmonaut',490,3,65]},f]

with open['cards','rb'] as g:
    id_cards = load[g]
print 'id_cards before change==',id_cards

id_cards['Jim'][0] = 'Wizard'

with open['cards','w'] as h:
    dump[id_cards,h]

with open['cards'] as e:
    id_cards = load[e]
print '\nid_cards after change==',id_cards
8
from cPickle import dump, load

with open['cards','wb'] as f:
    dump[{'Dan':['Warrior',500,1,0],'Jim':['dragonfly',300,2,10],'Somo':['cosmonaut',490,3,65]},f]

with open['cards','rb'] as g:
    id_cards = load[g]
print 'id_cards before change==',id_cards

id_cards['Jim'][0] = 'Wizard'

with open['cards','w'] as h:
    dump[id_cards,h]

with open['cards'] as e:
    id_cards = load[e]
print '\nid_cards after change==',id_cards
9
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
5
with open['example.txt','r',encoding='utf-8'] as file:
    data = file.readlines[]
1

with open

# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
0
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
1
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
2
with open['example.txt','r',encoding='utf-8'] as file:
    data = file.readlines[]
7
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
4
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
5
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
6
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
7
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
8
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
9

from cPickle import dump, load

with open['cards','wb'] as f:
    dump[{'Dan':['Warrior',500,1,0],'Jim':['dragonfly',300,2,10],'Somo':['cosmonaut',490,3,65]},f]

with open['cards','rb'] as g:
    id_cards = load[g]
print 'id_cards before change==',id_cards

id_cards['Jim'][0] = 'Wizard'

with open['cards','w'] as h:
    dump[id_cards,h]

with open['cards'] as e:
    id_cards = load[e]
print '\nid_cards after change==',id_cards
0
# creation
with open['pers.txt','wb'] as g:
    g.write['Dan \n Warrior \n 500 \r\n 1 \r 0 \n Jim  \n  dragonfly\r300\r2\n10\r\nSomo\ncosmonaut\n490\r\n3\r65']

with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt before treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt before treatment:\n',h.read[]


# treatment
def ripli[file_name,who,what]:
    with open[file_name,'rb+'] as f:
        ch = f.read[]
        x,y = re.search['^\s*'+who+'\s*[\r\n]+[[^\r\n]+]',ch,re.MULTILINE].span[1]
        f.seek[x]
        f.write[what+ch[y:]]
ripli['pers.txt','Jim','Wizard']


# after treatment
with open['pers.txt','rb'] as h:
    print 'exact content of pers.txt after treatment:\n',repr[h.read[]]
with open['pers.txt','rU'] as h:
    print '\nrU-display of pers.txt after treatment:\n',h.read[]
8
print[data]
6

Output:

['Line 1\n', 'Here is my modified Line 2\n', 'Line 3']

Sau khi sửa đổi:

Bài Viết Liên Quan

Chủ Đề