I'm fairly new to python, and I need to perform some data munging. I want some advice on the best practice for this: libraries, modules, better code to implementment, or just direction.
So I have text file with data organised in the following format:
A:100 B:200 C:300
A:150 B:350 C:370
I.e. each line represents related data, 3 variables, named A/B/C. Each new line shows data regarding the same variables but related to another object.
So my data is in a text file, and I want to output it in the following format:
100 '\t' 150
200 '\t' 350
300 '\t' 370
I.e. rows of data for variables A, B and C, tabulated, such that I can export to a graphing toolkit (probably Origin Pro).
This is the code I've come up with thus far:
with open("example.txt", 'r') as file:
for line in file.readlines():
line = line.replace(' ', '\n')
line = line.split(':', 1)[-1]
print line
This transforms the data into the following:
100
B:200
C:300
150
B:350
C:370
As the .split() is obviously only performed on each line prior to using .replace() to add new lines between the data. I feel like after I use .replace(), I need to start looping over the lines again to perform .split() or even just line[2:] iteratively, to remove the leading variable names - but then I can't think how I'm going to tabulate the data also for each line to create columns?
Any ideas? Thanks!