我想從文本文件中獲取一行的第三個元素,但是在嘗試它時我得到了上述錯誤。以下是我的代碼期望一個字符緩衝區對象: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