第二列有多个值,需要换行输出,并加上第一行
刚学习python,拜托大佬指点一二:示例数据
block1 137tbca|TbCa042341---137tbca|TbCa042342---137tbca|TbCa042343---137tbca|TbCa042344---137tbca|TbCa042345---137tbca|TbCa042346
block2 137tbca|TbCa042363---137tbca|TbCa042364---137tbca|TbCa042365---137tbca|TbCa042366---137tbca|TbCa042367---137tbca|TbCa042368
想要得到的结果:
block1 137tbca|TbCa042341
block1 137tbca|TbCa042342
block1 137tbca|TbCa042343
block1 137tbca|TbCa042344
block1 137tbca|TbCa042345
block1 137tbca|TbCa042346
block2 137tbca|TbCa042363
block2 137tbca|TbCa042364
block2 137tbca|TbCa042365
block2 137tbca|TbCa042366
block2 137tbca|TbCa042367
block2 137tbca|TbCa042368
自己拙劣的几句脚本:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
args=sys.argv
input1=args
output1=args
with open(input1, 'r') as fr1, open(output1,'w') as fw1:
file = fr1.readlines()
for line in file:
line = line.strip()
lines = line.split('\t')
new_line =lines+'\t'+lines[-1].replace('---', '\n')
print (new_line, file = fw1)
自己脚本输出的结果:
block1137tbca|TbCa042341
137tbca|TbCa042342
137tbca|TbCa042343
137tbca|TbCa042344
137tbca|TbCa042345
137tbca|TbCa042346
block2137tbca|TbCa042363
137tbca|TbCa042364
137tbca|TbCa042365
137tbca|TbCa042366
137tbca|TbCa042367
137tbca|TbCa042368
(只有第一行输出正确了,不知道怎么写,求鱼友们帮忙~)
感恩~~
for line in file:
line = line.strip()
title = line.split(' ')
word = line.split(' ')[-1]
for each in word.split('---'):
print(title,' '*8,each,sep='') 逃兵 发表于 2021-12-24 17:01
感恩,又学会了一点点,祝圣诞快乐~
页:
[1]