16. 练习:读写文件

在你之前看到的代码中, f.read() 调用没有传入参数。它自动变成从当前位置读取文件的所有剩余内容,即整个文件。如果向 .read() 传入整型参数,它将读取长度是这么多字符的内容,输出所有内容,并使 'window' 保持在该位置以准备继续读取。

with open(camelot.txt) as song:
    print(song.read(2))
    print(song.read(8))
    print(song.read())

输出:

We
're the 
knights of the round table
We dance whenever we're able

这样的话,在打开的文件中到处移动会很困难,因为没有太多可以导航的标记。

文本块中的 \n 是换行符。换行符表示一行的结束,告诉程序(例如文本编辑器)转到下一行。但是,对于文件中的一系列字符来说, \n 只是另一个字符。
幸运的是,Python 知道这些是特殊字符,你可以要求 Python 每次读取一行。我们来试试!

读取下一行

QUESTION:

请参阅 Python 文档的相关部分 ,找到读取文件下一行的方法。将方法的名称填入方框中。

SOLUTION:

readline

很方便的是,Python 将使用语法 for line in file 循环访问文件中的各行内容。
我可以使用该语法创建列表中的行列表。因为每行依然包含换行符,因此我使用 .strip() 删掉换行符。

camelot_lines = []
with open("camelot.txt") as f:
    for line in f:
        camelot_lines.append(line.strip())

print(camelot_lines)

输出:

["We're the knights of the round table", "We dance whenever we're able"]

练习:《飞翔的马戏团》 演员名单

你将创建一个演员名单,列出参演电视剧《巨蟒剧团之飞翔的马戏团》的演员。

写一个叫做 create_cast_list 的函数,该函数会接受文件名作为输入,并返回演员姓名列表。
它将运行文件 flying_circus_cast.txt (信息收集自 imdb.com)。文件的每行包含演员姓名、逗号,以及关于节目角色的一些(凌乱)信息。你 只需 提取姓名,并添加到列表中。你可以使用 .split() 方法 处理每行。

Start Quiz:

def create_cast_list(filename):
    cast_list = []
    #use with to open the file filename
    #use the for loop syntax to process each line
    #and add the actor name to cast_list

    return cast_list

cast_list = create_cast_list('flying_circus_cast.txt')
for actor in cast_list:
    print(actor)
Graham Chapman,  Various / ... (46 episodes, 1969-1974)
Eric Idle,  Various / ... (46 episodes, 1969-1974)
Terry Jones,  Various / ... (46 episodes, 1969-1974)
Michael Palin,  It's Man / ... (46 episodes, 1969-1974)
Terry Gilliam,  Various / ... (46 episodes, 1969-1974)
John Cleese,  Announcer / ... (40 episodes, 1969-1973)
Carol Cleveland,  Various / ... (34 episodes, 1969-1974)
Ian Davidson,  Algy Braithwaite / ... (8 episodes, 1969-1970)
John Hughman,  Alfred Lord Tennyson / ... (8 episodes, 1970-1974)
The Fred Tomlinson Singers,  Amantillado Chorus / ... (7 episodes, 1969-1973)
Connie Booth,  Animated Mother / ... (6 episodes, 1969-1974)
Bob Raymond,  'Dad' / ... (5 episodes, 1974)
Lyn Ashley,  Algon Girl / ... (5 episodes, 1970-1972)
Rita Davies,  Argument Secretary / ... (4 episodes, 1969-1972)
Stanley Mason,  Clapper Man / ... (4 episodes, 1970-1971)
David Ballantyne,  Ivan the Terrible / ... (3 episodes, 1970-1971)
Donna Reading,  Girl in Bikini with Its Man / ... (3 episodes, 1969)
Peter Brett,  Door-to-Door Martial Arts Salesman (2 episodes, 1974)
Maureen Flanagan,  Anona Winn / ... (2 episodes, 1969-1970)
Katya Wyeth,  Elsie / ... (2 episodes, 1969)
Frank Lester,  The Late Professor Thynne (2 episodes, 1972-1974)
Neil Innes,  Hesitant guitarist / ... (2 episodes, 1974)
Dick Vosburgh,  Van der Berg (1 episode, 1969)
Sandra Richards,  'Semprini' Girl / ... (1 episode, 1970)
Julia Breck,  Puss In Boots / ... (1 episode, 1972)
Nicki Howorth,  Miss Bladder (1 episode, 1972)
Jimmy Hill,  Himself (1 episode, 1974)
Barry Cryer,  Herman Rodrigues (1 episode, 1969)
Jeannette Wild,  Second Secretary (1 episode, 1970)
Marjorie Wilde,  Dear Old Lady (1 episode, 1970)
Marie Anderson,  Girl interviewing the announcer (1 episode, 1972)
Caron Gardner,  Mary (1 episode, 1973)
Nosher Powell,  Jack Bodell (1 episode, 1973)
Carolae Donoghue,  Vera's Husband's Mistress (1 episode, 1969)
Vincent Wong,  Mr. Kamikaze (1 episode, 1970)
Helena Clayton,  Various Roles (1 episode, 1971)
Nigel Jones,  Various (1 episode, 1972)
Roy Gunson, (1 episode, 1970)
Daphne Davey,  Various Roles (1 episode, 1971)
Stenson Falke, (1 episode, 1974)
Alexander Curry,  Various (1 episode, 1970)
Frank Williams,  Clerk of the Court (1 episode, 1972)
Ralph Wood, (1 episode, 1970)
Rosalind Bailey,  Elizabethan Girl (1 episode, 1972)
Marion Mould, (1 episode, 1974)
Sheila Sands,  Stripper / ... (uncredited) (2 episodes, 1969)
Richard Baker,  Himself - BBC News Anchor (uncredited) (3 episodes, 1972-1973)
Douglas Adams,  Dr. Emile Koning - Surgeon / ... (uncredited) (2 episodes, 1974)
Ewa Aulin,  Harrassed Woman (uncredited) (1 episode, 1969)
Reginald Bosanquet,  Himself (uncredited) (1 episode, 1970)
Barbara Lindley,  Bride (uncredited) (1 episode, 1970)
Roy Brent,  Armoured Knight (uncredited) (1 episode, 1972)
Jonas Card,  Armoured Knight (uncredited) (1 episode, 1972)
Tony Christopher,  Armoured Knight (uncredited) (1 episode, 1972)
Beulah Hughes, (uncredited) (1 episode, 1972)
Peter Kodak,  Armoured Knight (uncredited) (1 episode, 1972)
Lulu,  Herself (uncredited) (1 episode, 1972)
Jay Neill,  Armoured Knight (uncredited) (1 episode, 1972)
Graham Skidmore,  Armoured Knight (uncredited) (1 episode, 1972)
Ringo Starr,  Himself (uncredited) (1 episode, 1972)
Fred Tomlinson,  Superintendent McGough (uncredited) (1 episode, 1972)
David Hamilton,  Himself - Thames TV Announcer (uncredited) (1 episode, 1973)
Suzy Mandel,  German Girl (uncredited) (1 episode, 1974)
Peter Woods,  BBC Presenter (uncredited) (1 episode, 1974)