Python有关文件读写方面的问题

img

类似如下代码,供参考和修改:

import random
random.seed(1000)
def f_write():
    for i in range(10):
        s=','.join(map(str,[random.randint(-50,50) for _ in range(random.randint(3,8))]))
        with open('data_1124.txt','a',encoding='utf-8') as f:
            f.writelines(s+'\n')


def f_read():
    with open('data_1124.txt', 'r', encoding='utf-8') as fr:
        maxx=-50
        for x in fr.read().strip().split('\n'):
            for y in x.split(','):
                a=int(y)
                if a>maxx:
                    maxx=a
    return maxx
def f_readlines():
    with open('data_1124.txt', 'r', encoding='utf-8') as fl:
        maxl=-400
        for x in fl.read().strip().split('\n'):
            a=0
            for y in x.split(','):
                a+=int(y)                
            if a>maxl:
                maxl=a
    return maxl
f_write()
print(f_read())
print(f_readlines())

如对你有帮助,请点击采纳按钮。