在 python 中使用字符串
elif data.find('PRIVMSG') != -1:
message = ':'.join(data.split (':')[2:])
if message.lower().find('code') == -1:
nick = data.split('!')[ 0 ].replace(':',' ')
destination = ''.join (data.split(':')[:2]).split (' ')[-2]
function = message.split( )[0]
print nick + ' : ' + function
arg = data.split( )
args = ''
for index,item in enumerate(arg) :
if index > 3 :
if args == '':
args = item
else :
args += ' ' + item
if data.find('.topic') != -1:
nick = data.split('!')[ 0 ].replace(':','')
for line in open('masters.txt'):
if nick in line:
sck.send('TOPIC ' + " " + chan + " " + args + '\r\n')
当我尝试执行诸如 .topic 1 2 3 4 5 6 7 8 9 10
之类的操作时,它会将频道主题更改为 3 4 5 6 7 8 9 10
整个事情的 1 2 3 4 5 6 7 8 9 10
我想知道为什么它从第三个字符串开始,而不是从开始?我需要从绳子上分开或剥掉一些东西吗?
elif data.find('PRIVMSG') != -1:
message = ':'.join(data.split (':')[2:])
if message.lower().find('code') == -1:
nick = data.split('!')[ 0 ].replace(':',' ')
destination = ''.join (data.split(':')[:2]).split (' ')[-2]
function = message.split( )[0]
print nick + ' : ' + function
arg = data.split( )
args = ''
for index,item in enumerate(arg) :
if index > 3 :
if args == '':
args = item
else :
args += ' ' + item
if data.find('.topic') != -1:
nick = data.split('!')[ 0 ].replace(':','')
for line in open('masters.txt'):
if nick in line:
sck.send('TOPIC ' + " " + chan + " " + args + '\r\n')
When I try to do something like .topic 1 2 3 4 5 6 7 8 9 10
it changes the topic of the channel to 3 4 5 6 7 8 9 10
instead of of the whole thing 1 2 3 4 5 6 7 8 9 10
I'm wondering why does it start from the 3rd string and not from the beginning? Do I need to split or strip something from the string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是显示问题的最小示例:
这都是由于
if index >; 3:
this is the minimal example that shows the problem:
it's all due to
if index > 3:
我想知道为什么它从第三个字符串开始,而不是从头开始?
因为这就是你告诉它要做的事情。
enumerate()
函数从 0 开始索引,因此索引 0 对应字符串'topic'
,索引 1 对应字符串'1'
,很快。因此,当您使用条件if index > 时3
,它将忽略前四个字符串(索引为 0、1、2 和 3 的字符串,在您的情况下为字符串'topic'
、'1'、<代码>'2'
和<代码>'3')。首先,我们可以将循环大大简化为以下一行:
即对
arg
列表进行切片以获取从第四个条目开始的所有条目,然后使用join() 函数将其连接成一个由字符
' '
分隔的字符串。其次,如果您只想删除第一个条目并将其余条目连接到字符串中,只需将索引更改为 1:
如果不同的命令需要以不同的方式处理
arg
列表,那么您需要根据具体情况而不是在一个地方执行此操作:I'm wondering why does it start from the 3rd string and not from the beginning?
Because thats what you told it to do. The
enumerate()
function starts indexing from 0, so index 0 corresponds to the string'topic'
, index 1 to string'1'
and so on. Hence, when you use the conditionif index > 3
, it will ignore the first four strings (those with indices 0, 1, 2, and 3, in your case the strings'topic'
,'1'
,'2'
, and'3'
).Firstly, we can greatly simplify the loop to the following single line:
That is, slice the
arg
list to get all entries from the fourth entry onwards, and then use thejoin()
function to concatenate it into a string separated by the character' '
.Secondly, if you only want to remove the first entry and join the rest into the string, simply change the index to 1:
If different commands need to handle the
arg
list differently, then you'll need to do this on a case-by-case basis rather than in one place:我修复了代码中的错误。
这个有效,我需要在声明通道变量后添加
" :"
以便它可以正确读取数据。I fixed the bug in my code.
this one works, I needed to add
" :"
after declaring the channel variable so it can read the data properly.