简体   繁体   中英

expected a character buffer object: Python 2.7

I am trying to get the third element of a line from a text file, but while trying it I get the above mentioned error. Following is my code

def time_difference():
    button_pressed = ''
    command = ''
    b_p_c_s = ''
    next_line_to_command = ''
    test = ''
    print "\n\033[32m-- TIME DIFFERENCES --\033[0m\n"
    with open('messages', 'r') as searchfile:
        for line in searchfile:
            if ':Button' and 'pressed' in line:
                button_pressed = str(line)
                with open('buttonpress_and_commandstarted','a') as buttonpressed:
                    buttonpressed.write(button_pressed)
                buttonpressed.close()
            elif 'Command' and 'started' in line:
                command = str(line)
                with open('buttonpress_and_commandstarted','a') as commandstarted:
                    commandstarted.write(command)
                buttonpressed.close()
        with open('buttonpress_and_commandstarted','a') as file_opened:
            file_opened.close()
        with open('buttonpress_and_commandstarted','r') as buttonpress_commandstarted:
            b_p_c_s = buttonpress_commandstarted.read()
            print b_p_c_s
        buttonpress_commandstarted.close()
        print "\n\033[32m-- NEXT TO KEYWORDS --\033[0m\n"
        with open('buttonpress_and_commandstarted', 'r') as searchfile:
            for line in searchfile:
                try:
                    if 'Command' and 'started' in line:
                        next_line_to_command = searchfile.next()
                        test = str(next_line_to_command)
                        print next_line_to_command
                except StopIteration:
                    pass
            print "\n\033[32m-- TIME --\033[0m\n"
            test.split(3) 
        searchfile.close()

I get

Traceback (most recent call last):
  File "./gui-analyser.py", line 114, in <module>
    time_difference()
  File "./gui-analyser.py", line 106, in time_difference
    test.split(3) 
TypeError: expected a character buffer object

My intention was to get the third element after the space in the line for example

Jan 01 07:24:07 AMIRA-134500021 user.notice butler[775]: LOG:200708a0:12:Button 11 pressed.

In this case 07:24:07

I saw some forums its because split can be done only using string so I casted the variable to string yet I get this issue. Any guidance will be really helpful.

str.split() takes, as its first argument, a string to split the input string on. You are trying to pass it an integer instead.

If you wanted to split on whitespace and pick out a specific element from the result, then index the returned list:

third_element = test.split()[2]

where Python uses 0-based indexing, so the third element is accessed by using 2 as the index. Note that this returns the value, so you'll probably want to store that result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM