python输出每五个一行_如何输出文件中的每一行python

当在数据中找到'!masters'时,打开'masters.txt'文件。原代码只打印第一行,现修正为使用迭代器逐行读取并打印文件内容,同时发送到指定通道。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

if data.find('!masters') != -1:

f = open('masters.txt')

lines = f.readline()

for line in lines:

print lines

sck.send('PRIVMSG ' + chan + " " + str(lines) + '\r\n')

f.close()

masters.txt has a list of nicknames, how can I print every line from the file at once?. The code I have only prints the first nickname. Your help will be appreciate it. Thanks.

解决方案

Firstly, as @l33tnerd said, f.close should be outside the for loop.

Secondly, you are only calling readline once, before the loop. That only reads the first line. The trick is that in Python, files act as iterators, so you can iterate over the file without having to call any methods on it, and that will give you one line per iteration:

if data.find('!masters

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值