python - 只读特定行

标签 python python-3.x

我只想读取某些行,例如 0 到 10 范围内的行,程序应该检查用户输入的数据的正确性。

from ftplib import FTP
from ftplib import error_perm

start = 0 # some starting index
end = 5

with open('string.txt', 'rb') as password_list:
     for i,line in enumerate(password_list):
       if(i>start & i<end):
        line=line.replace(b'\n',b'').replace(b'\r',b'')
        password=line.decode("utf-8")

最佳答案

试试这个:

start = 0
end = 5
with open('string.txt', 'rb') as fh:
   lines = [i.replace(b'\n',b'').replace(b'\r',b'') for i in fh.readlines()][start:end]
   passwords = [i.decode('utf-8') for i in lines]

关于python - 只读特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58104967/

相关文章:

python - 如何在运行时在 python 中创建文件句柄?

python - 非常具体的子串检索和分割

Python验证函数生成IndexError : list assignment index out of range

python - 如何使用多处理 python 更新和检索图像?

python-3.x - 我的 matplot 的网格偏离了 -0.5

python - 我该如何编写这个 django 查询?

python - 如何在将行推送到 pgsql 之前验证它是否合规?

python - 从 Anaconda 安装 Keras 库时出现问题

python - Plon:与非文件夹对象相关的python脚本index_html

linux - 保护 python 源代码