Python 如何删除/“数字”;来自输出的数字
我需要从输出中删除补丁集编号,这意味着从1293927/2 1293929/3中删除“/位”,我已在下面显示了预期输出,输出应如下所示,但应为列表类型。。如何分割这个Python 如何删除/“数字”;来自输出的数字,python,Python,我需要从输出中删除补丁集编号,这意味着从1293927/2 1293929/3中删除“/位”,我已在下面显示了预期输出,输出应如下所示,但应为列表类型。。如何分割这个 sql_get = """SELECT gerrit_id FROM gerrits.gerrit_submit_table where (SI='%s' and component='%s' and release_bit =
sql_get = """SELECT gerrit_id
FROM gerrits.gerrit_submit_table
where (SI='%s'
and component='%s' and release_bit = '0' and picked_bit = '0')"""%(SI,component)
#print sql_get
rows = cursor.execute(sql_get)
gerrits = cursor.fetchall()
#print "gerrits"
#print gerrits -->prints (('1293927/2',), ('1293929/2',))
print' '.join(item[0] for item in gerrits).rstrip('\r\n') --> prints 1293927/2 1293929/2
输出:-
1293927/2 1293929/2
预期产出:-
1293927 1293929
使用
re
模块
import re
print ' '.join(re.sub(r'/.*', '',item[0]) for item in gerrits).rstrip('\r\n')
或
使用string.split
函数
print ' '.join(item[0].split('/')[0] for item in gerrits).rstrip('\r\n')
更新:
l = []
for item in gerrits:
m = item[0].split('/')[0]
l.append(m)
print l
我会考虑使用<代码> SPLIT()/代码>:
这将消除rsplit的需要,并处理斜杠后的任意数量的数字。请注意,它输出一个字符串
,但我假设这就是给定的join()
所要查找的
新规定:
输出为列表:
print [item[0].split('/')[0] for item in gerrits)]
print'。为gerrits中的项加入(项[0][:-2])。rstrip('\r\n')
是从数据库中获取正确数据的方法吗?如果是这样,那么该部分就完全不相关了。print'。对于gerrits中的项,join(re.sub(r'/\d+$,'',item[0])。rstrip('\r\n')
是否有方法将输出作为列表而不是字符串发送?输出仍然应该是1293927 1293929,但它应该是list类型是否有方法将其作为列表而不是字符串发送?print[item[0]。拆分('/')[0]用于gerrits中的项]
我的意思是输出仍然应该是1293927 1293929
,但它应该是list类型。我已经编写了它。检查它:类型([item[0]。拆分('/')[0]用于gerrits中的项])
你能编辑你的答案吗?输出仍然应该是1293927 1293929
,但应该是列表类型,上面的输出将是['1293927','1293929']
print [item[0].split('/')[0] for item in gerrits)]