将文本文件作为字典

时间:2017-10-23 11:29:45

标签: python dictionary input

使用Python 3.

我必须编写一个带有一个参数(字符串)的函数,并且必须从包含序列(键)和序列(值)名称的txt文件中返回一个字典。键和值都必须是字符串。

文本文件:

Read1 GGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTCGTCCAGACCCCTAGC
Read2 CTTTACCCGGAAGAGCGGGACGCTGCCCTGCGCGATTCCAGGCTCCCCACGGG
Read4 TGCGAGGGAAGTGAAGTATTTGACCCTTTACCCGGAAGAGCG
Read3 GTCTTCAGTAGAAAATTGTTTTTTTCTTCCAAGAGGTCGGAGTCGTGAACACATCAGT
Read5 CGATTCCAGGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTC
Read6 TGACAGTAGATCTCGTCCAGACCCCTAGCTGGTACGTCTTCAGTAGAAAATTGTTTTTTTCTTCCAAGAGGTCGGAGT

我走到这一步,但我想我错过了一些东西,我不知道我的工作是否正确。我已经标记了行(带#),我怀疑它是否正确。

def read_data(file_name):
    input_file=open(sequencing_reads.txt)
    #sequence_dict={}
    for line in input_file:
        #x=line.split(",")
    #return sequence_dict
    input_file.close()

我知道它必须返回包含以下内容的字典:

{'Read1': 'GGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTCGTCCAGACCCCTAGC',
 'Read2': 'CTTTACCCGGAAGAGCGGGACGCTGCCCTGCGCGATTCCAGGCTCCCCACGGG',
 'Read4': 'TGCGAGGGAAGTGAAGTATTTGACCCTTTACCCGGAAGAGCG',
 'Read3': 'GTCTTCAGTAGAAAATTGTTTTTTTCTTCCAAGAGGTCGGAGTCGTGAACACATCAGT',
 'Read5': 'CGATTCCAGGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTC',
 'Read6': 'TGACAGTAGATCTCGTCCAGACCCCTAGCTGGTACGTCTTCAGTAGAAAATTGTTTTTTTCTTCCAAGAGGTCGGAGT'}

你能帮我填补空白吗?

编辑:我需要保持简单,所以请不要导入包和智能技巧: - )

编辑2:

我也试过这个:

with open('sequencing_reads.txt', 'r') as document:
    answer = {}
    for line in document:
        line = line.split()
        if not line:  
            continue
        answer[line[0]] = line[1:]
print(answer)

输出结果为:

{'Read1': ['GGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTCGTCCAGACCCCTAGC'], 'Read2': ['CTTTACCCGGAAGAGCGGGACGCTGCCCTGCGCGATTCCAGGCTCCCCACGGG'], 'Read4': ['TGCGAGGGAAGTGAAGTATTTGACCCTTTACCCGGAAGAGCG'], 'Read3': ['GTCTTCAGTAGAAAATTGTTTTTTTCTTCCAAGAGGTCGGAGTCGTGAACACATCAGT'], 'Read5': ['CGATTCCAGGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTC'], 'Read6': ['TGACAGTAGATCTCGTCCAGACCCCTAGCTGGTACGTCTTCAGTAGAAAATTGTTTTTTTCTTCCAAGAGGTCGGAGT']}

如何摆脱序列周围的“[]”?

EDIT4:

def read_data(file_name):
    with open("sequencing_reads.txt", "r") as document:
        answer = {}
        for line in document:
            line = line.split()
            if not line:
                continue
                answer[line[0]] = line[1:]
                final_answer = {a:b[0] for a, b in answer.items()}
final_answer = read_data("sequencing_reads.txt")
print(final_answer)

打印:

None

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

import re
def read_data(file_name):
   data = open(file_name).read()
   keys = [filter(lambda x:bool(x), i)[0][1:-1] for i in re.findall("{(.*?)\:|(?<=,\n\s)(.*?)\:", data)]
   values = [filter(lambda x:bool(x), i)[0][1:-1] for i in re.findall('(?<=:\s)(.*?)(?=,\n)|(?<=\s)(.*?)(?=})', data)]
   final_data = {a:b for a, b in zip(keys, values)}
   return final_data

输出:

{'Read1': 'GGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTCGTCCAGACCCCTAGC', 'Read3': 'GTCTTCAGTAGAAAATTGTTTTTTTCTTCCAAGAGGTCGGAGTCGTGAACACATCAGT', 'Read2': 'CTTTACCCGGAAGAGCGGGACGCTGCCCTGCGCGATTCCAGGCTCCCCACGGG', 'Read5': 'CGATTCCAGGCTCCCCACGGGGTACCCATAACTTGACAGTAGATCTC', 'Read4': 'TGCGAGGGAAGTGAAGTATTTGACCCTTTACCCGGAAGAGCG', 'Read6': "'Read6': 'TGACAGTAGATCTCGTCCAGACCCCTAGCTGGTACGTCTTCAGTAGAAAATTGTTTTTTTCTTCCAAGAGGTCGGAGT"}

编辑:

import ast 
def read_data(file_name):
   final_data = ast.literal_eval(open(file_name).read())
   return final_data

编辑1:关于删除括号,只需通过索引来访问该值:

final_answer = {a:b[0] for a, b in answer.items()}
print(final_answer)

如果您在打印read_data返回的值时遇到问题,可以尝试以下操作:

answer = read_data("the_file.txt")
print(answer)

编辑3:

def read_data(file_name):
   with open(file_name, "r") as document:
      answer = {}
      for line in document:
         line = line.split()
         if line:
            answer[line[0]] = line[1:]
      return {a:b[0] for a, b in answer.items()}

print(read_data("sequencing_reads.txt"))

答案 1 :(得分:1)

您的文件"sequencing_reads.txt"采用json格式。您可以使用python标准库中的json模块将内容轻松加载到字典中。

import json

with open("sequencing_reads.txt") as f:
    sequence_dict = json.load(f)

答案 2 :(得分:1)

首先,如果你的文件是json格式并且是单独的行,你应该把它读成一行,可能是这样的:

def read_data(file_name):
    lines = open(file_name).readlines()
    merged_line = " ".join([line.strip() for line in lines])

其次,json.loads需要字符串的双引号(例如:{“a”:“a”})。如果您使用单引号(如您的示例中所示),则可能存在错误。所以你可以这样做:

# 1,use json.loads, but replace first
import json
merged_line = merged_line.replace("'", '"')
data = json.loads(merged_line)

# 2,use ast
import ast
data = ast.literal_eval(merged_line)