Python123第七周编程题

1,文本的平均列数

with open ('latex.log','r') as fo:
    c = 0
    r = 0
    for line in fo:
        line = line.replace("\n",'')
        if line == '':
            continue
        c = c + len(line) #总有效字符数
        r = r + 1 #总行数
    print(round(c/r))

2,CSV格式清洗与转换

f = open("data.csv""r")
ls = f.readlines()
ls = ls[::-1]
lt = []
for item in ls:
    item = item.strip("\n") #删去每行结尾的回车
    item = item.replace(" ", "") #将每行首尾空格剔除
    lt = item.split(",")
    lt = lt[::-1]
    print(";".join(lt))
f.close()

你可能感兴趣的:(python)