leer archivo y crear diccionario basado en delimitadores

i have a file that has several lines consisting of items. The structure is a class, followed by a category, followed by prerequisites in the class.

#Class, Category, Pre-requisites(amount of them can change)

MATH 2430, preprofessional, Math 2429|Math 2428,
Math 2431, professional, Math 2430|Math 2429|Math 2428,

what i want to end up having is a dictionary with the class as a key, then the category and prerequisites as the values in a list. something like

{'MATH 2430' : ['preprofessional', 'Math 2429','Math 2428']...... }

The vertical bar is an indicator of oncoming prerequisite classes.The problem im running into is that the number or vertical bar delimiters may vary so the pre prequisite classes may vary by line. So im not sure how to split based on how many vertical bars there are

i wrote 
zdic = {}
pre_req = file.count("|") # to count how many vertical bars appear
if "|" in file :
prereq = pre_req
for line in file :
    course, category, prereq1(depending on bars...) = split("\W+",file)

how do i deal with the fact that the number of pre requisite classes may vary? and depending on how many there are, split accordingly to manipulate and enter into a dixtionary?

preguntado el 27 de noviembre de 13 a las 01:11

Does the file have that trailing comma? -

yea each line is followed by that comma -

2 Respuestas

Algo como esto:

txt='''\
MATH 2430, preprofessional, Math 2429|Math 2428,
Math 2431, professional, Math 2430|Math 2429|Math 2428,'''

d={}
for line in txt.splitlines():
    line=line.rstrip(',')
    li=[e.strip() for e in line.split(',')]
    d[li[0]]=[li[1]]+li[2].split('|')

print d 
# {'MATH 2430': ['preprofessional', 'Math 2429', 'Math 2428'], 'Math 2431': ['professional', 'Math 2430', 'Math 2429', 'Math 2428']}

O, mejor aún, usa csv:

import csv

d={}
with open('/tmp/test.csv') as f:
    for line in csv.reader(f, skipinitialspace=True):
        d[line[0]]=[line[1]]+line[2].split('|')
print d
# {'MATH 2430': ['preprofessional', 'Math 2429', 'Math 2428'], 'Math 2431': ['professional', 'Math 2430', 'Math 2429', 'Math 2428']}

respondido 27 nov., 13:02

Solo usa el split method. Assuming you have the last portion (containing the prerequisites) of the line you are parsing, you don't really need to count anything if you use the split method with the proper delimiter (in this case a |). Por ejemplo,

Caso 1:

>>> pre_req = "Math 2430|Math 2429|Math 2428"
>>> pre_req.split("|")
['Math 2430', 'Math 2429', 'Math 2428']

Caso 2:

>>> pre_req = "Math 2429|Math 2428"
>>> pre_req.split("|")
['Math 2429', 'Math 2428']

split will split the the string and give you all the prerequisites as a list of strings, no matter how many there are.

Here's a glimpse of you could go about parsing any given line. I've used the strip y split métodos.

>>> line = "MATH 2430, preprofessional, Math 2429|Math 2428,"
>>> line = line.strip().split(",") # This gives you a list of strings
>>> d = {}
>>> d[line[0]] = [line[1]] + line[2].strip().split("|")
>>> d
{'MATH 2430': [' preprofessional', 'Math 2429', 'Math 2428']}

respondido 27 nov., 13:02

but i want the class as a key so i need the comma as a seperator, then i want the pre requisites to be followed as a value in a list - Zach Santiago

@ZachSantiago split applies there as well. I've added a small snippet to demonstrate that. - control deslizante

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.