mercredi 4 février 2015

Remove unique values in textfile - if else - Python

I have a text file looking like the following:



A B C D
0 4 3 5
3 3 5 8
8 1 5 7
9 3 7 9


The data is sorted by column C. What I am trying to do, is to write a Python (3.4) that deletes each line, where the value in column C does not equal the value of column C in another line. So I need to pull out all lines, that have one or more matching values of column C. The mockup code below, is my attempt to show what I want to do:



For loop
if lineXcolumnY == lineX2columnY2
OR
if lineX2columnY2 == lineXcolumnY
print line X
Else
Delete line X


On the above example of data, the code would then give me:



A B C D
3 3 5 8
8 1 5 7


I am a complete newbie to Python, so what is confusing me a lot, is how to actually refer to the text file in the script, and how to refer to a specific column. In R I would do Data$C to refer to the column, but in Python?


Aucun commentaire:

Enregistrer un commentaire