首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python - readlines

Python - readlines
EN

Stack Overflow用户
提问于 2020-05-12 03:50:56
回答 1查看 57关注 0票数 0

有没有一种方法可以让python读取行,只使用前10个字符,然后将它们赋给一个变量?

代码语言:javascript
复制
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

我不想把静态删除行变量

EN

回答 1

Stack Overflow用户

发布于 2020-05-12 04:04:17

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61738104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档