获取文本文件中制表符后的所有内容如何做到?

huangapple go评论59阅读模式
英文:

How do I get everything after tab in text file?

问题

I have a text file i am trying to isolate everything after the tab. How do I do that 

Before:
GATTTCAAAAGCATTCTGTTGTTCTTTGAGGTCAGCAACCTGACCAATAAAAA
TACGCTTGACAAGACGTCTCCATGTCCTTTCAAGCGACTTGCTACGCTATGCACCATGC
AAAATTGGATGCATAAAACAAAATAAATGTAAATACTAAAATAATGATCATATTCTTACCTTAATCCGT
英文:

I have a text file i am trying to isolate everything after the tab. How do I do that

Before:

GSVIVT01034325001_1 GATTTCAAAAGCATTCTGTTGTTCTTTGAGGTCAGCAACCTGACCAATAAAAA
GSVIVT01034326001_2 TACGCTTGACAAGACGTCTCCATGTCCTTTCAAGCGACTTGCTACGCTATGCACCATGC
GSVIVT01034329001_3 AAAATTGGATGCATAAAACAAAATAAATGTAAATACTAAAATAATGATCATATTCTTACCTTAATCCGT

After:

GATTTCAAAAGCATTCTGTTGTTCTTTGAGGTCAGCAACCTGACCAATAAAAA
TACGCTTGACAAGACGTCTCCATGTCCTTTCAAGCGACTTGCTACGCTATGCACCATGC
AAAATTGGATGCATAAAACAAAATAAATGTAAATACTAAAATAATGATCATATTCTTACCTTAATCCGT
fhandle: IO = open("seqs.txt","r")
list_of_let: List[str] = []

for items in fhandle:
    spltl: List[str] = items.split("\t")
    new_let: str = str(spltl[0])
    list_of_let.append(new_let)
print(list_of_let)

答案1

得分: 1

你可以使用str.split()。您不需要指定\t字符,split会自动使用空格进行分割:

with open('your file.txt', 'r') as f_in:
    for line in map(str.strip, f_in):
        # 跳过空行:
        if line == "":
            continue
        _, part_2 = line.split(maxsplit=1)
        print(part_2)

输出:

GATTTCAAAAGCATTCTGTTGTTCTTTGAGGTCAGCAACCTGACCAATAAAAA
TACGCTTGACAAGACGTCTCCATGTCCTTTCAAGCGACTTGCTACGCTATGCACCATGC
AAAATTGGATGCATAAAACAAAATAAATGTAAATACTAAAATAATGATCATATTCTTACCTTAATCCGT
英文:

You can use str.split(). You don't have to specify the \t char, split will automatically work with whitespaces:

with open('your file.txt', 'r') as f_in:
    for line in map(str.strip, f_in):
        # skip empty lines:
        if line == "":
            continue
        _, part_2 = line.split(maxsplit=1)
        print(part_2)

Prints:

GATTTCAAAAGCATTCTGTTGTTCTTTGAGGTCAGCAACCTGACCAATAAAAA
TACGCTTGACAAGACGTCTCCATGTCCTTTCAAGCGACTTGCTACGCTATGCACCATGC
AAAATTGGATGCATAAAACAAAATAAATGTAAATACTAAAATAATGATCATATTCTTACCTTAATCCGT

huangapple
  • 本文由 发表于 2023年2月24日 08:54:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定