从 Python 2.7 中的打印中删除括号和引号 [英] Removing brackets and quotes from print in Python 2.7
问题描述
我正在尝试使用 Python 2.7 从打印语句中删除括号我尝试了来自各种论坛的建议,但没有按预期工作 最终想到自己在这里提问.
代码:
with open('buttonpress_and_commandstarted','r') as buttonpress_commandstarted:对于 buttonpress_commandstarted 中的行:button_press_command_time = ''如果 os.path.getsize('buttonpress_and_commandstarted') >0:button_press_command_time = line.split()[2]别的:打印"> 无法获取时间戳,因为文件为空"button_press = re.findall(r"\:12\:(.*?)\.",line)command_executed = re.findall(r"\:4\:(.*?) started\.",line)使用 open('timestamp_buttons_and_commands', 'w') 作为 timestamp_buttons_and_commands:timestamp_buttons_and_commands.write(str(button_press_command_time) + str(button_press) + str(command_executed))使用 open("timestamp_buttons_and_commands", "r+") 作为 timestamp_buttons_and_commands:内容 = timestamp_buttons_and_commands.readlines()从字符串导入连接结果 = ''.join(内容)打印结果
我不确定我在做什么错误.我得到以下输出
00:22:12['按钮 9 按下'][]00:22:13['按下按钮 9'][]00:22:14['按下按钮 9'][]00:22:15['按下按钮 9'][]00:22:15[]['命令媒体编解码器(2)']00:22:17['按下按钮 9'][]00:22:19['按下按钮 9'][]00:22:19[]['命令集传感器格式(3)']00:22:22[]['CDC']00:22:22[]['命令你好']00:22:22[]['命令你好']00:22:22[]['命令你好']00:22:22[]['命令你好']00:22:22[]['命令你好']00:22:25['按下按钮 10'][]
但我不想要括号和引号
re.findall
返回一个列表.当你执行 str(someList)
时,它会打印括号和逗号:
如果你想在没有[
和,
的情况下打印,使用join
:
如果你想在没有[
但有,
的情况下打印:
如果你想保留 '
,你可以使用 repr
和列表理解:
<小时>
在您编辑之后,您的列表中似乎只有 1 个元素.所以你只能打印第一个元素:
<预><代码>>>>l = ['a']>>>打印 l['一种']>>>打印 l[0]一种I am trying to remove the brackets from the print statement using Python 2.7 I tried suggestions from various forums but it didn't work as expected finally thought of asking out here myself.
Code:
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
I am not sure what mistake I am doing.I get the following output
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'][]
But I don't want the brackets and quotes
re.findall
returns a list. When you do str(someList)
, it will print the brackets and commas:
>>> l = ["a", "b", "c"]
>>> print str(l)
['a', 'b', 'c']
If you want to print without [
and ,
, use join
:
>>> print ' '.join(l)
a b c
If you want to print without [
but with ,
:
>>> print ', '.join(l)
a, b, c
And if you want to keep the '
, you could use repr
and list comprehension:
>>> print ', '.join(repr(i) for i in l)
'a', 'b', 'c'
After your edit, it seems that there is only 1 element in your lists. So you can only print the first element:
>>> l = ['a']
>>> print l
['a']
>>> print l[0]
a
这篇关于从 Python 2.7 中的打印中删除括号和引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!