Search code examples
pythonpython-itertools

Manipulating data in csv using itertools (python)


I'm trying to add a feature to the code below but seem to be messing up somewhere.

The following code basically repeats the first "z" table columns for each of the following speakers (like a excel transposition). For these columns after "z" are dates and just want a date column.

The problem is that the python script generates the values ​​correctly but need it insert the column talking about what that date, repeat this information to all the values ​​that date.

Python script:

from itertools chain from import

f = open ("C:\\Test.CSV", "r")
sep = ""
M = []
M = [M + [s.strip () for s in line.split (sep)] for line in f.readlines ()]
f.close ()

i = 0 # title
w = 4 # cols in title
r = 1 # body
z = 4 # fields fix

result = [M [i] [w] + ["date"] + ["Value"]] + list (chain (* [[x [: z] + [y] for y in x [z + 1: ]] for x in M ​​[A:]]))

f = open ("C:\out.csv", 'w')
f.writelines ("\ n" .join ("". join (s) for s in result) + "\ 0")
f.close ()

Something (input):

[[X; y; date1; date2; date3]
[01; 02; 03; 04; 05]
[06; 07; 08; 09; 10]
[11; 12; 13, 14 15]]

The current script does this correctly (output now):

[[X; y; date1]
[01; 02; 03]
[01; 02; 04]
[01; 02; 05]
[06; 07; 08]
[06; 07; 09]
[06; 07; 10]
[11; 12; 13]
[11; 12; 14]
[11; 12; 15]]

I take the output is this (desired output):

[[X; y; date; value; ]
[01; 02; date1; 03]
[01; 02; date2; 04]
[01; 02; date3; 05]
[06; 07; date1; 08]
[06; 07; date2; 09]
[06; 07; date3; 10]
[11; 12; date1; 13]
[11; 12; date2; 14]
[11; 12; date3; 15]]

Has anyone done something like that?


Solution

  • Any reason you can't just use csv?

    import csv
    
    f = open("iter.csv", "r")
    g = open("out.csv", "w", newline="")
    transpose_columns = ['date1', 'date2', 'date3']
    target_columns = ['date', 'value']
    reader = csv.DictReader(f)
    writer = csv.DictWriter(g, fieldnames=[a for a in reader.fieldnames
                                           if a not in transpose_columns] + target_columns,
                            extrasaction='ignore')
    writer.writeheader()
    for row in reader:
        for col in transpose_columns:
            row[target_columns[0]] = col
            row[target_columns[1]] = row[col]
            writer.writerow(row)
    f.close()
    g.close()