我想根据文件的内容对原始文件进行排序,并在该列中获得唯一的元素:
原始文件:
qoow_12_xx7_21 wer1 rwty3
asss_x17_211 aqe3 sda4
acyi_112_werxc xcu12 weqa1
qwer_234_ssd aqe3 wers
输出排序数据:
asss_x17_211 aqe3 sda4
qwer_234_ssd aqe3 wers
qoow_12_xx7_21 wer1 rwty3
acyi_112_werxc xcu12 weqa1
输出唯一列2:
aqe3
wer1
xcu12
我的尝试无效代码:
from operator import itemgetter
import itemgetter
def get_unique(data):
seen=""
for e in data:
if e not in seen:
seen="\t".join(seen)
return seen
col2=""
with open("myfile.txt", "r") as infile, open("out.xls","w") as outfile:
for line in infile:
data=line.rstrip.split("\t")
sorted_data=sorted(data, key=lambda e: e.itemgetter)
col2="".join(data[1])
uniq_col2=get_unique(col2)
outfile.write(sorted_data)# tab-delimited sorted data
outfile.write(uniq_col2) # sorted column 2 data
有人能帮我把这个代码写出来吗。谢谢
试试这个:
相关问题 更多 >
编程相关推荐