在 python 中使用字符串

发布于 2024-11-02 07:05:54 字数 1035 浏览 1 评论 0原文

   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

極樂鬼 2024-11-09 07:05:54

这是显示问题的最小示例:

     data = ".topic 1 2 3 4 5 6 7 8 9 10"
     arg = data.split()
     args = ''
     for index,item in enumerate(arg):
          if index > 3:
              if args == '':
                  args = item
              else:
                  args += ' ' + item
     print args

这都是由于 if index >; 3:

this is the minimal example that shows the problem:

     data = ".topic 1 2 3 4 5 6 7 8 9 10"
     arg = data.split()
     args = ''
     for index,item in enumerate(arg):
          if index > 3:
              if args == '':
                  args = item
              else:
                  args += ' ' + item
     print args

it's all due to if index > 3:

尾戒 2024-11-09 07:05:54

我想知道为什么它从第三个字符串开始,而不是从头开始?

因为这就是你告诉它要做的事情。 enumerate() 函数从 0 开始索引,因此索引 0 对应字符串 'topic',索引 1 对应字符串 '1',很快。因此,当您使用条件 if index > 时3,它将忽略前四个字符串(索引为 0、1、2 和 3 的字符串,在您的情况下为字符串 'topic''1'、<代码>'2' 和<代码>'3')。

首先,我们可以将循环大大简化为以下一行:

args = ' '.join(arg[4:])

即对 arg 列表进行切片以获取从第四个条目开始的所有条目,然后使用 join() 函数将其连接成一个由字符 ' ' 分隔的字符串。

其次,如果您只想删除第一个条目并将其余条目连接到字符串中,只需将索引更改为 1:

args = ' '.join(arg[1:])

如果不同的命令需要以不同的方式处理 arg 列表,那么您需要根据具体情况而不是在一个地方执行此操作:

arg = data.split()

if '.topic' in data:
    nick = data.split('!')[ 0 ].replace(':','')
    topic = ' '.join(arg[1:])
    for line in open('masters.txt'):
        if nick in line:
            sck.send('TOPIC %s %s\r\n' % (chan, topic))

elif '.bannick' in data:
    nick_to_ban = arg[0]
    length = arg[1]
    reason = ' '.join(arg[2:])
    for line in open('masters.txt'):
        if nick in line:
            sck.send('BAN %s %s %s %s\r\n' % (chan, nick_to_ban, length, reason))

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 condition if 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:

args = ' '.join(arg[4:])

That is, slice the arg list to get all entries from the fourth entry onwards, and then use the join() 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:

args = ' '.join(arg[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:

arg = data.split()

if '.topic' in data:
    nick = data.split('!')[ 0 ].replace(':','')
    topic = ' '.join(arg[1:])
    for line in open('masters.txt'):
        if nick in line:
            sck.send('TOPIC %s %s\r\n' % (chan, topic))

elif '.bannick' in data:
    nick_to_ban = arg[0]
    length = arg[1]
    reason = ' '.join(arg[2:])
    for line in open('masters.txt'):
        if nick in line:
            sck.send('BAN %s %s %s %s\r\n' % (chan, nick_to_ban, length, reason))
静若繁花 2024-11-09 07:05:54

我修复了代码中的错误。

  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')

这个有效,我需要在声明通道变量后添加 " :" 以便它可以正确读取数据。

I fixed the bug in my code.

  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')

this one works, I needed to add " :" after declaring the channel variable so it can read the data properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文