2014-11-25 2 views
-1

Python 2.7을 사용하여 print 문에서 대괄호를 제거하려고합니다. 여러 포럼에서 제안을 시도했지만 기대했던대로 작동하지 않았습니다.Python 2.7에서 대괄호와 따옴표를 제거하는 방법

코드 :

with open('buttonpress_and_commandstarted','r') as buttonpress_commandstarted: 
     for line in buttonpress_commandstarted: 
      button_press_command_time = '' 
      if os.path.getsize('buttonpress_and_commandstarted') > 0:    
       button_press_command_time = line.split()[2] 
      else: 
       print " > Cannot get time stamp as the file is empty" 
      button_press = re.findall(r"\:12\:(.*?)\.",line) 
      command_executed = re.findall(r"\:4\:(.*?) started\.",line) 
      with open('timestamp_buttons_and_commands', 'w') as timestamp_buttons_and_commands: 
       timestamp_buttons_and_commands.write(str(button_press_command_time) + str(button_press) + str(command_executed)) 

      with open("timestamp_buttons_and_commands", "r+") as timestamp_buttons_and_commands: 
       contents = timestamp_buttons_and_commands.readlines() 
       from string import join 
       result = ''.join(contents) 
      print result 

내가 뭘해야 다음과 같은 출력

00:22:12['Button 9 pressed'][] 
00:22:13['Button 9 pressed'][] 
00:22:14['Button 9 pressed'][] 
00:22:15['Button 9 pressed'][] 
00:22:15[]['Command MediaCodec (2)'] 
00:22:17['Button 9 pressed'][] 
00:22:19['Button 9 pressed'][] 
00:22:19[]['Command SetSensorFormat (3)'] 
00:22:22[]['CDC'] 
00:22:22[]['Command Hello'] 
00:22:22[]['Command Hello'] 
00:22:22[]['Command Hello'] 
00:22:22[]['Command Hello'] 
00:22:22[]['Command Hello'] 
00:22:25['Button 10 pressed'][] 

를 얻을 누구인지 실수 확실하지 않다 그러나 나는

+0

일반 팁 : 코드 –

+0

의 중간에 망가 가져 오기 모듈은 물론 당신의 문제가 무엇인지 분명하지 않다 당신에게 @TimCastelijns – user89

+0

감사합니다 - 당신이 아래의 코드를 줄일 수 [최소 예] (http://stackoverflow.com/help/mcve) 입력과 예상 및 실제 출력을 제공합니까? – jonrsharpe

답변

0

re.findall를 브래킷을 원하는 인용하지 않는다 목록을 반환합니다. 당신이 [,없이 인쇄 할 경우

>>> l = ["a", "b", "c"] 
>>> print str(l) 
['a', 'b', 'c'] 

, join 사용 :

>>> print ' '.join(l) 
a b c 

당신이 [ 않고 ​​있지만 ,을 함께 인쇄 할 경우 str(someList)을 할 때, 괄호와 쉼표를 인쇄합니다 :

>>> print ', '.join(l) 
a, b, c 

'을 유지하려면 repr 및 목록 이해를 사용 울드 : 편집 한 후

>>> print ', '.join(repr(i) for i in l) 
'a', 'b', 'c' 

를, 당신의 목록 만 한 요소가 보인다. 그래서 당신은 첫 번째 요소를 인쇄 할 수 있습니다

>>> l = ['a'] 
>>> print l 
['a'] 
>>> print l[0] 
a 
+0

이 lines'button_press = ''.join (button_press) command_executed = ''.join (command_executed) timestamp_buttons_and_commands.write (STR (button_press_command_time)를 추가 삭제됩니다 GO2 @ 감사합니다 (button_press) + str (command_executed))'트릭을 했어! 정말 고맙습니다 :) – user89

관련 문제