英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论