二手产品经理

二手产品经理

THIS IS RENO

操作文件 - 49天 - 在线学python100天

记录#

Catbox

  1. 今天继续学习文件操作的文件读取,函数 open (url,r)
  2. 使用 read () 函数获取文件内容,一般是将内容赋值给变量。
  3. 变量获取到内容之后,使用 close () 函数关闭文件操作。
  4. 使用 readline () 读取文件中的一行数据。
  5. 使用 while 循环读取文件中所有文件,文件中最后一行是空,所以 if 内容 ==“” 时 break 就可以退出循环。
  6. 今天的练习是从 high.score 中读取数据,判断哪一行的数字最高,然后输出。
  7. if value == "" 这种判断为空不是好的判断方式,好的应为: if not value。

CODE#

import time

print("🌟Current Leader🌟")
print("Analyzing high scores......")
time.sleep(2)

f = open("high.score", "r")
numinfo = 1
while True:
  contents = f.readline().strip().split()
  if not contents:
    break
  if int(contents[1]) > numinfo:
    numinfo = int(contents[1])
    numname = contents[0]
f.close()
print(f"Current leader is {numname} {numinfo}")


加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。