views:

29

answers:

3

I'm writing a script that will print a random line of text from a file into an XCHAT channel. So far it works fine, but I want to add one last bit of functionality.

I have logs with, for example, "Oct 23 12:07:59 (nickname> " appearing before every line of text. I just want to print the parts of the lines that follow the "(nickname>", how can I do this?

__module_name__ = "ran.py"
__module_version__ = "1.0"
__module_description__ = "script to add random text to channel messages"

import xchat
import random

def ran(message):
    message = random.choice(open("E:/logs/myfile.log", "r").readlines())
    return(message)


def ran_cb(word, word_eol, userdata):
    message = ''
    message = ran(message)
    xchat.command("msg %s %s"%(xchat.get_info('channel'), message))
    return xchat.EAT_ALL


xchat.hook_command("ran", ran_cb, help="/ran to use")
+1  A: 

If the first > character is exactly the place to split, then try:

toBeIgnored, toBeUsed = line.split('>', 1)
eumiro
A: 

I think > can be used as separator, so:

_, message = message.split('>', 1)

will split line into 2 part, first part (underscore while it is not important) with "Oct 23 12:07:59 (nickname" and second with text after >

You can use it in ran():

def ran(message):
    message = random.choice(open("E:/logs/myfile.log", "r").readlines())
    _, message = message.split('>', 1)
    return(message)
Michał Niklas
Where would I place this in the script?
guy
In `ran()` function (I updated answer)
Michał Niklas
I receive the following error when I do this: File "D:\irc\xchat\scripts\ran.py", line 10 _, message = message.split('>', 1) ^ IndentationError: unexpected indent Error loading module D:\irc\xchat\scripts\ran.py
guy
Ohh, I see. You are not the author of original code. In Python we must care about line intendation. Check if your code is intended with spaces or with tabs. All the lines in `ran()` method must be intended by equal count of spaces (for example 4 spaces) or with tab character. In my answer all is intended by 4 spaces.
Michał Niklas
A: 

I would have asked in the comments but I don't have enough rep. Does/Can your user's nicknames contain ">" character? If not, you can use the split command:

message = random.choice(open("E:/logs/myfile.log", "r").readlines())
text = message.split("> ", 1)[1]
return(text)

Or you can use a single line:

message = random.choice(open("E:/logs/myfile.log", "r").readlines()).split("> ", 1)[1]
return(message)
Gani Simsek
Where would I actually place this in the script?
guy
You can use it wherever you want, however, since the reading from file happens in ran(message) function you probably want to return it there. I edited my answer.
Gani Simsek
I received the same error as I did using the other solution posted: File "D:\irc\xchat\scripts\ran.py", line 10 _, message = message.split('>', 1) ^ IndentationError: unexpected indent Error loading module D:\irc\xchat\scripts\ran.py
guy
It says you've indentation error, check that line, use spaces instead of tabs.
Gani Simsek