繁体   English   中英

Python - readlines

[英]Python - readlines

有没有办法让 python 读取行并且只使用前 10 个字符,然后将它们分配给一个变量?

Remove_example= 'example'
Remove_at= '@'
#Remove_Agent='

filepath = 'Older WinCollect Agents on Version 728.txt'
with open(filepath) as fp:
   line = fp.readline()
   print(line[0][10])
   cnt = 1
   while line:
       print("Line {}: {}".format(cnt, line.strip(),).replace(str(Remove_example),'').replace(str(Remove_at),''))
       line = fp.readline()
       cnt += 1

我不想把 static 删除行变量

def get_line_fronts(filepath: str) -> list:
    """Get list of first 10 characters in each line in a file."""
    line_fronts = []
    with open(filepath, "r") as fh:
        for line in fh:
            line_fronts.append(line[:10])
    return line_fronts


filepath = 'Older WinCollect Agents on Version 728.txt'
for line_front in get_line_fronts(filepath):
    # do whatever

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM