-2

I have txt file like this:

Name  |  Class  |  Points
--------------------------
Name1 |    (2)  |  30
Name1 |    (3)  |  50
Name1 |    (5)  |  15
Name2 |    (1)  |  25
Name2 |    (3)  |  88
Name2 |    (4)  |  3

There are 100 unique values under the column class: [(1), (2), (3), ..., (100)]

I would like to populate the values based on the class value, so the new table will have the classes as the columns like this:

      |  (1)  |   (2)  |  (3) | (4) | (5) |...
----------------------------------------------
Name1 |       |  30    |  50  |     | 15  |
Name2 |   25  |        |  88  |  3  |     |

Explanation for Name1:

In the first table (the original format from the file), the Name1 has 30 points for class (2), 50 points for class (3), and 15 points for class (5)

sergiobuj
  • 2,318
  • 2
  • 21
  • 24

1 Answers1

0

So far I have this:

f = open("file.txt", "r")
classes = set()
names = set()
for line in f:
    line = line.split("\t")
    if line[1] == "Class":
        continue
    else:
        classes.add(int(line[1]))
    names.add(line[0])
sorted(classes)
print(classes)
with open("new_file.txt", "a") as f:
    for i in classes:
        f.write(f"\t{i}")
    for j in names:
        f.write(f"\n{j}")