python操作txt文件中数据教程[1]-使用python读写txt文件
觉得有用的话,欢迎一起讨论相互学习~
- 原始txt文件
- 程序实现后结果
程序实现
filename = './test/test.txt'
contents = []
DNA_sequence = []
with open(filename, 'r') as f:
for line in f.readlines():
contents.append(line)
f.close()
for content in contents:
p = 0
for bit in content:
if bit == " ":
DNA_sequence.append(content[0:p])
break
else:
p = p + 1
"""
['AAACAAGGAACAAACGCACA', 'AAAGGACACAGTGAGAGACG', 'AGCCATTGTCGAGTCCGTTA', 'GTGGTCACTCCTCGCAAATT', 'TTCAACCATACAGGCCTCGT',
'CAAATGTGAGGATTCGGACG', 'CCGTGGTGAACTGGAGCGTT', 'Sum', 'AAAGGACACAGTGAGAGACG', 'AGCCATTGTCGAGTCCGTTA',
'CCACAACGCTCGAAGGCAAG', 'AAGTACAGCGGGCCAATAGC', 'CAAATGTGAGGATTCGGACG', 'GAGAACGTTGAGTGAGCGTG', 'GATGTTAAGTAGAGCAGAGG',
'Sum', 'AAACAAGGAACAAACGCACA', 'AAAGGACACAGTGAGAGACG', 'CCACAACGCTCGAAGGCAAG', 'AAGTACAGCGGGCCAAGATC',
'CTCAGAAGATCTCGATGGCT', 'AGCCATTGTCGAGTCCGTTA', 'TGCCGCAAACTACACACACG', 'Sum']
"""
for var in DNA_sequence:
if var == "Sum":
DNA_sequence.remove(var)
"""
['AAACAAGGAACAAACGCACA', 'AAAGGACACAGTGAGAGACG', 'AGCCATTGTCGAGTCCGTTA', 'GTGGTCACTCCTCGCAAATT', 'TTCAACCATACAGGCCTCGT',
'CAAATGTGAGGATTCGGACG', 'CCGTGGTGAACTGGAGCGTT', 'AAAGGACACAGTGAGAGACG', 'AGCCATTGTCGAGTCCGTTA', 'CCACAACGCTCGAAGGCAAG',
'AAGTACAGCGGGCCAATAGC', 'CAAATGTGAGGATTCGGACG', 'GAGAACGTTGAGTGAGCGTG', 'GATGTTAAGTAGAGCAGAGG', 'AAACAAGGAACAAACGCACA'
, 'AAAGGACACAGTGAGAGACG', 'CCACAACGCTCGAAGGCAAG', 'AAGTACAGCGGGCCAAGATC', 'CTCAGAAGATCTCGATGGCT',
'AGCCATTGTCGAGTCCGTTA', 'TGCCGCAAACTACACACACG']
"""
file_write_obj = open("./DNAlog.txt", 'w')
for var in DNA_sequence:
file_write_obj.writelines(var)
file_write_obj.writelines('\n')
file_write_obj.close()