【发布时间】:2014-11-25 11:01:33
【问题描述】:
我正在尝试使用 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'][]
但我不想要括号和引号
【问题讨论】:
-
一般提示:不要在代码中间导入模块
-
当然谢谢@TimCastelijns
-
不清楚您的问题是什么 - 您能否将代码缩减为 minimal example 并提供输入以及预期和实际输出?
-
另外,我认为您对
join的使用是错误的。您从string导入join并且从不使用它,而是使用内置的,这是相同的。因此,导入是没有意义的。 -
好的,谢谢@go2 那我就删了
标签: python-2.7 readfile text-parsing