Python string split() method causes index error when reading IRCConvert bytes to a string?What does the 'b' character do in front of a string literal?Split a string by spaces — preserving quoted substrings — in PythonDoes Python have a string 'contains' substring method?Split a string by a delimiter in pythonSplit string with multiple delimiters in Pythonproblem with xmlrpc serverPyAudio Over Network crashesSplit string on whitespace in PythonSplitting on last delimiter in Python string?Python: IndexError: list index out of range with IRC BotPython IRC bot for Twitch receiving two messages in one output

Why were Er and Onan punished if they were under 20?

Is Trump personally blocking people on Twitter?

Does throwing a penny at a train stop the train?

Maximum charterer insertion

How to achieve this rough borders and stippled illustration look?

Flatten array with OPENJSON: OPENJSON on a value that may not be an array? [ [1] ], vs [1]

<schwitz>, <zwinker> etc. Does German always use 2nd Person Singular Imperative verbs for emoticons? If so, why?

If your plane is out-of-control, why does military training instruct releasing the joystick to neutralize controls?

What are the bumps on the Vega rocket

Is there any word for "disobedience to God"?

ESTA: "Is your travel to the US occurring in transit to another country?" when going on a cruise

Book where the stars go black due to aliens stopping human observation collapsing quantum possibilities

Why isn't there research to build a standard lunar, or Martian mobility platform?

During copyediting, journal disagrees about spelling of paper's main topic

Should I intentionally omit previous work experience when applying for jobs?

Graduate student with abysmal English writing skills, how to help

What species of wasp is this? And how to get rid of them?

Did Lincoln tell Stowe "So you're the little woman that started this great war!"?

Setting MAC field to all-zero to indicate unencrypted data

Get ids only where one id is null and other isn't

Single word for "refusing to move to next activity unless present one is completed."

Is there a word for a message that is intended to be intercepted by an adversary?

Why are Hobbits so fond of mushrooms?

Matchmaker, Matchmaker, make me a match



Python string split() method causes index error when reading IRC


Convert bytes to a string?What does the 'b' character do in front of a string literal?Split a string by spaces — preserving quoted substrings — in PythonDoes Python have a string 'contains' substring method?Split a string by a delimiter in pythonSplit string with multiple delimiters in Pythonproblem with xmlrpc serverPyAudio Over Network crashesSplit string on whitespace in PythonSplitting on last delimiter in Python string?Python: IndexError: list index out of range with IRC BotPython IRC bot for Twitch receiving two messages in one output






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I'm creating a twitch chat bot to read the chat on my stream. But when I try to .split() the incoming string into separate strings to isolate the username and message, it displays an extra ' and ["'"]. when I try to print the strings separately by index I get an index error.



Following is the code which connects the the twitch chat fine, and the result when I type "test" into the chat.



from settings import *
import socket
import threading

class twitch:
def __init__(self, host, port, nick, pwd, channel):
self.s = socket.socket()
self.s.connect((host, port))
self.s.send(bytes("PASS " + pwd + "rn", "UTF-8"))
self.s.send(bytes("NICK " + nick + "rn", "UTF-8"))
self.s.send(bytes("JOIN #" + channel + " rn", "UTF-8"))
self.s.send(bytes("PRIVMSG #" + channel + " :" + "Connected " + "rn", "UTF-8"))
self.alive = True

readerthread = threading.Thread(target=self.read_chat)
readerthread.start()

def read_chat(self):
while self.alive:
for line in str(self.s.recv(1024)).split('\r\n'):
if "PING :tmi.twitch.tv" in line:
print(time.strftime("%H:%M:%S"), "PONG :tmi.twitch.tv")
s.send(bytes("PONG :tmi.twitch.tvrn", "UTF-8"))
else:
print(line)
parts = line.split(":")
print(parts)

def main():
tc = twitch(HOST, PORT, NICK, PASS, CHANNEL)


Printing the string (line) to the console produces: b':username!username@username.tmi.twitch.tv PRIVMSG #username :test



However when I split the string and print the list of strings (parts) it produces this:
["b'", 'username!username@username.tmi.twitch.tv PRIVMSG #username ', 'test']
'
["'"]










share|improve this question




























    -1















    I'm creating a twitch chat bot to read the chat on my stream. But when I try to .split() the incoming string into separate strings to isolate the username and message, it displays an extra ' and ["'"]. when I try to print the strings separately by index I get an index error.



    Following is the code which connects the the twitch chat fine, and the result when I type "test" into the chat.



    from settings import *
    import socket
    import threading

    class twitch:
    def __init__(self, host, port, nick, pwd, channel):
    self.s = socket.socket()
    self.s.connect((host, port))
    self.s.send(bytes("PASS " + pwd + "rn", "UTF-8"))
    self.s.send(bytes("NICK " + nick + "rn", "UTF-8"))
    self.s.send(bytes("JOIN #" + channel + " rn", "UTF-8"))
    self.s.send(bytes("PRIVMSG #" + channel + " :" + "Connected " + "rn", "UTF-8"))
    self.alive = True

    readerthread = threading.Thread(target=self.read_chat)
    readerthread.start()

    def read_chat(self):
    while self.alive:
    for line in str(self.s.recv(1024)).split('\r\n'):
    if "PING :tmi.twitch.tv" in line:
    print(time.strftime("%H:%M:%S"), "PONG :tmi.twitch.tv")
    s.send(bytes("PONG :tmi.twitch.tvrn", "UTF-8"))
    else:
    print(line)
    parts = line.split(":")
    print(parts)

    def main():
    tc = twitch(HOST, PORT, NICK, PASS, CHANNEL)


    Printing the string (line) to the console produces: b':username!username@username.tmi.twitch.tv PRIVMSG #username :test



    However when I split the string and print the list of strings (parts) it produces this:
    ["b'", 'username!username@username.tmi.twitch.tv PRIVMSG #username ', 'test']
    '
    ["'"]










    share|improve this question
























      -1












      -1








      -1








      I'm creating a twitch chat bot to read the chat on my stream. But when I try to .split() the incoming string into separate strings to isolate the username and message, it displays an extra ' and ["'"]. when I try to print the strings separately by index I get an index error.



      Following is the code which connects the the twitch chat fine, and the result when I type "test" into the chat.



      from settings import *
      import socket
      import threading

      class twitch:
      def __init__(self, host, port, nick, pwd, channel):
      self.s = socket.socket()
      self.s.connect((host, port))
      self.s.send(bytes("PASS " + pwd + "rn", "UTF-8"))
      self.s.send(bytes("NICK " + nick + "rn", "UTF-8"))
      self.s.send(bytes("JOIN #" + channel + " rn", "UTF-8"))
      self.s.send(bytes("PRIVMSG #" + channel + " :" + "Connected " + "rn", "UTF-8"))
      self.alive = True

      readerthread = threading.Thread(target=self.read_chat)
      readerthread.start()

      def read_chat(self):
      while self.alive:
      for line in str(self.s.recv(1024)).split('\r\n'):
      if "PING :tmi.twitch.tv" in line:
      print(time.strftime("%H:%M:%S"), "PONG :tmi.twitch.tv")
      s.send(bytes("PONG :tmi.twitch.tvrn", "UTF-8"))
      else:
      print(line)
      parts = line.split(":")
      print(parts)

      def main():
      tc = twitch(HOST, PORT, NICK, PASS, CHANNEL)


      Printing the string (line) to the console produces: b':username!username@username.tmi.twitch.tv PRIVMSG #username :test



      However when I split the string and print the list of strings (parts) it produces this:
      ["b'", 'username!username@username.tmi.twitch.tv PRIVMSG #username ', 'test']
      '
      ["'"]










      share|improve this question














      I'm creating a twitch chat bot to read the chat on my stream. But when I try to .split() the incoming string into separate strings to isolate the username and message, it displays an extra ' and ["'"]. when I try to print the strings separately by index I get an index error.



      Following is the code which connects the the twitch chat fine, and the result when I type "test" into the chat.



      from settings import *
      import socket
      import threading

      class twitch:
      def __init__(self, host, port, nick, pwd, channel):
      self.s = socket.socket()
      self.s.connect((host, port))
      self.s.send(bytes("PASS " + pwd + "rn", "UTF-8"))
      self.s.send(bytes("NICK " + nick + "rn", "UTF-8"))
      self.s.send(bytes("JOIN #" + channel + " rn", "UTF-8"))
      self.s.send(bytes("PRIVMSG #" + channel + " :" + "Connected " + "rn", "UTF-8"))
      self.alive = True

      readerthread = threading.Thread(target=self.read_chat)
      readerthread.start()

      def read_chat(self):
      while self.alive:
      for line in str(self.s.recv(1024)).split('\r\n'):
      if "PING :tmi.twitch.tv" in line:
      print(time.strftime("%H:%M:%S"), "PONG :tmi.twitch.tv")
      s.send(bytes("PONG :tmi.twitch.tvrn", "UTF-8"))
      else:
      print(line)
      parts = line.split(":")
      print(parts)

      def main():
      tc = twitch(HOST, PORT, NICK, PASS, CHANNEL)


      Printing the string (line) to the console produces: b':username!username@username.tmi.twitch.tv PRIVMSG #username :test



      However when I split the string and print the list of strings (parts) it produces this:
      ["b'", 'username!username@username.tmi.twitch.tv PRIVMSG #username ', 'test']
      '
      ["'"]







      python string irc twitch






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 26 at 3:28









      PileOfMeatballsPileOfMeatballs

      93 bronze badges




      93 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You are reading bytes. Hence the b'...'.
          What does the 'b' character do in front of a string literal?





          Convert it to a string and then handle it.
          Convert bytes to a string?





          code from the link.



          >>> b"abcde"
          b'abcde'

          # utf-8 is used here because it is a very common encoding, but you
          # need to use the encoding your data is actually in.
          >>> b"abcde".decode("utf-8")
          'abcde'





          share|improve this answer























          • Thank you very much for the response. I tried adding x = line.decode("UTF-8") and i get: AttributeError: 'str' object has no attribute 'decode'

            – PileOfMeatballs
            Mar 26 at 22:42












          • for line in str(self.s.recv(1024)).split('\r\n'): you appear to be casting to string. Remove the cast. for line in (self.s.recv(1024)).split('\r\n'): Then decode bytes

            – thatNLPguy
            Mar 27 at 1:24











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349445%2fpython-string-split-method-causes-index-error-when-reading-irc%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You are reading bytes. Hence the b'...'.
          What does the 'b' character do in front of a string literal?





          Convert it to a string and then handle it.
          Convert bytes to a string?





          code from the link.



          >>> b"abcde"
          b'abcde'

          # utf-8 is used here because it is a very common encoding, but you
          # need to use the encoding your data is actually in.
          >>> b"abcde".decode("utf-8")
          'abcde'





          share|improve this answer























          • Thank you very much for the response. I tried adding x = line.decode("UTF-8") and i get: AttributeError: 'str' object has no attribute 'decode'

            – PileOfMeatballs
            Mar 26 at 22:42












          • for line in str(self.s.recv(1024)).split('\r\n'): you appear to be casting to string. Remove the cast. for line in (self.s.recv(1024)).split('\r\n'): Then decode bytes

            – thatNLPguy
            Mar 27 at 1:24
















          0














          You are reading bytes. Hence the b'...'.
          What does the 'b' character do in front of a string literal?





          Convert it to a string and then handle it.
          Convert bytes to a string?





          code from the link.



          >>> b"abcde"
          b'abcde'

          # utf-8 is used here because it is a very common encoding, but you
          # need to use the encoding your data is actually in.
          >>> b"abcde".decode("utf-8")
          'abcde'





          share|improve this answer























          • Thank you very much for the response. I tried adding x = line.decode("UTF-8") and i get: AttributeError: 'str' object has no attribute 'decode'

            – PileOfMeatballs
            Mar 26 at 22:42












          • for line in str(self.s.recv(1024)).split('\r\n'): you appear to be casting to string. Remove the cast. for line in (self.s.recv(1024)).split('\r\n'): Then decode bytes

            – thatNLPguy
            Mar 27 at 1:24














          0












          0








          0







          You are reading bytes. Hence the b'...'.
          What does the 'b' character do in front of a string literal?





          Convert it to a string and then handle it.
          Convert bytes to a string?





          code from the link.



          >>> b"abcde"
          b'abcde'

          # utf-8 is used here because it is a very common encoding, but you
          # need to use the encoding your data is actually in.
          >>> b"abcde".decode("utf-8")
          'abcde'





          share|improve this answer













          You are reading bytes. Hence the b'...'.
          What does the 'b' character do in front of a string literal?





          Convert it to a string and then handle it.
          Convert bytes to a string?





          code from the link.



          >>> b"abcde"
          b'abcde'

          # utf-8 is used here because it is a very common encoding, but you
          # need to use the encoding your data is actually in.
          >>> b"abcde".decode("utf-8")
          'abcde'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 26 at 3:51









          thatNLPguythatNLPguy

          1018 bronze badges




          1018 bronze badges












          • Thank you very much for the response. I tried adding x = line.decode("UTF-8") and i get: AttributeError: 'str' object has no attribute 'decode'

            – PileOfMeatballs
            Mar 26 at 22:42












          • for line in str(self.s.recv(1024)).split('\r\n'): you appear to be casting to string. Remove the cast. for line in (self.s.recv(1024)).split('\r\n'): Then decode bytes

            – thatNLPguy
            Mar 27 at 1:24


















          • Thank you very much for the response. I tried adding x = line.decode("UTF-8") and i get: AttributeError: 'str' object has no attribute 'decode'

            – PileOfMeatballs
            Mar 26 at 22:42












          • for line in str(self.s.recv(1024)).split('\r\n'): you appear to be casting to string. Remove the cast. for line in (self.s.recv(1024)).split('\r\n'): Then decode bytes

            – thatNLPguy
            Mar 27 at 1:24

















          Thank you very much for the response. I tried adding x = line.decode("UTF-8") and i get: AttributeError: 'str' object has no attribute 'decode'

          – PileOfMeatballs
          Mar 26 at 22:42






          Thank you very much for the response. I tried adding x = line.decode("UTF-8") and i get: AttributeError: 'str' object has no attribute 'decode'

          – PileOfMeatballs
          Mar 26 at 22:42














          for line in str(self.s.recv(1024)).split('\r\n'): you appear to be casting to string. Remove the cast. for line in (self.s.recv(1024)).split('\r\n'): Then decode bytes

          – thatNLPguy
          Mar 27 at 1:24






          for line in str(self.s.recv(1024)).split('\r\n'): you appear to be casting to string. Remove the cast. for line in (self.s.recv(1024)).split('\r\n'): Then decode bytes

          – thatNLPguy
          Mar 27 at 1:24









          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349445%2fpython-string-split-method-causes-index-error-when-reading-irc%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현