Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: How to extract data from .txt file with unspecific header

Rabbit is cute:

I have a .txt files which consists the data from multiple .txt file. It start with the filename then header(no. of headers are varies) and data. Examples are here. Anyone have any ideas to do this?

IMPORTANT: I need to leave a "-" when one of the students do not have the subject.

input.txt


Student 1.txt
Maths,90
Science,50
English,62
Student 2.txt
Maths,75
Science,80
Chinese,88
Student 3.txt
Maths,83
Chinese,22
English,90
Physics,56

Now I have the code below to make it into dict


open_input_file=open("input.txt","r")
datalines= open_input_file.readlines()
open_input_file.readlines()
open_input_file.close()
line=[]
value=0
from collections import defaultdict
d1=defaultdict(list)
for line in datalines:
if line.find(',')>-1:
key=line.split(",")[0]
value=line.split(",")[1].strip("\n")
d1[key].append(value)
else:
key="filename"
value=line
d1[key].append(value)

d=dict((key,tuple(value)) for key, value in d1.iteritems())
print d

and I get this


{'Chinese': ('88', '22'), 'Science': ('50', '80'), 'filename': ('Student 1.1\n', 'Student 2.1\n', 'Student 3.1\n'), 'English': ('62', '90'), 'Maths': ('90', '75', '83'), 'Physics': ('56',)}

but what I really want is something like this, so that the mark is correspond to the student number


filename,Student 1.txt, Student 2.txt, Student 3.txt
Maths,90,75,83
Science,50,80,-
English,62,-,90
Chinese,-,88,22
Physics,-,-,-,56



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: How to extract data from .txt file with unspecific header

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×