我有一个文件,我正在阅读列表。在此列表中,我需要分隔几个字段
如果该行中有R,则list1应具有以下输出
所需输出为列表:
[48998 18:45:44 qp32,
49005 00:44:23 qp32,
49047 312:33:0 qp128]
如果该行中有Q,那么另一个list2应该将剩余的条目作为输出
该文件如下:
job.txt-
48998.tyrone-clustergic1_nwgsmbupi18:45:44Rqp32
48999.tyrone-clustergic2_nwgsmbupi0Qbatch
49005.tyrone-cluster...01R-1849-01_2mcbkss00:44:23Rqp32
49032.tyrone-clustergaussian_top.shchemraja0Qqp32
8788.tyrone-clusterjet_egridasevelt312:33:0Rqp128
49052.tyrone-clustercase3sqTS1e-4mecvamsi0Qqp32
49053.tyrone-cluster...01R-1850-01_1mcbkss0Qbatch
49054.tyrone-cluster...01R-1850-01_2mcbkss0Qbatch
8987.tyrone-clusterjob256icrtaraf0Qqp256
49073.tyrone-clusterjob256icrtaraf0Qbatch
直到现在我已经尝试了
list1 = [(s.split(' ')[0]) for s in mylist if 'R' in s]
#for R和类似的Q
o/p : ['48999.tyrone-clustergic2_nwgsmbupi0Qbatch', '49032.tyrone-clustergaussian_top.shchemraja0Qqp32', '49052.tyrone-clustercase3sqTS1e-4mecvamsi0Qqp32', '49053.tyrone-cluster...01R-1850-01_1mcbkss0Qbatch', '49054.tyrone-cluster...01R-1850-01_2mcbkss0Qbatch']
但输出应采用我在开头
中指定的格式答案 0 :(得分:0)
with open('job.txt') as f:
list1=[]
list2=[]
for line in f.readlines():
if 'R' in line:
list1.append(line) #you might want to format this line as per your requirements using regex
else:
list2.append(line)
print list1
print list2
答案 1 :(得分:0)
job_file_handle = open("job.txt", "r") # open a handle on the file
job_lines = job_file_handle.readlines() # read in all of the lines
job_file_handle.close() # close the file handle
for line in job_lines:
if line.split("R") > line.split("Q"):
processRLine(line)
else
processQLine(line)