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;
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
add a comment |
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
add a comment |
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
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
python string irc twitch
asked Mar 26 at 3:28
PileOfMeatballsPileOfMeatballs
93 bronze badges
93 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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'
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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'
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
add a comment |
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'
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
add a comment |
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'
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'
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
add a comment |
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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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