我想从文本文件中获取一行的第三个元素,但是在尝试它时我得到了上述错误。以下是我的代码期望一个字符缓冲区对象:Python 2.7
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()
我得到
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
我的目的是在该行的空间,例如
Jan 01 07:24:07 AMIRA-134500021 user.notice butler[775]: LOG:200708a0:12:Button 11 pressed.
在这种情况下07:24之后获得的第三个元素:07
我看到一些论坛,因为拆分可以b e只使用字符串完成,所以我铸造变量字符串,但我得到这个问题。任何指导都会很有帮助。
你期望'test.split(3)'做什么? 'str.split()'需要一个字符串,而不是一个整数。 – 2014-11-21 17:29:23
您认为“split”的第一个参数意味着什么? – user2357112 2014-11-21 17:30:12
我想获得该行的第三个元素(即)空间后的第三个字符串。在这种情况下'Jan 01 07:24:32 AMIRA-134500021 user.notice管家[775]:LOG:200708a0:12:按钮22被按下.'我想检索07:24:32 – user89 2014-11-21 17:32:24