0

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?

NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
sn3fru
  • 106
  • 3
  • 12

1 Answers1

0

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()
Eric
  • 28
  • 5
  • Thank you very much for your help! My feedback to improve the script: 1) excerpt: newline = "" gave error and had to exclude 2) the last two columns appear repeatedly in the export 3) export it skips a line (a line comes with information and the other is blank, it may be because of the point of exclusion (1)? Again thank you, I found the csv pack better than intertools – sn3fru Jul 02 '15 at 12:35
  • @user1146 On my phone so a little hard to test some things but the newline="" is part of Python 3. This is due to a [Unix/Windows newline issue.](https://pythonconquerstheuniverse.wordpress.com/2011/05/08/newline-conversion-in-python-3/) That's why its giving an error in Python 2 and then giving you an extra line in the csv. [Instead open the file in binary mode ("wb" instead of "w") when you're in Python 2.](http://stackoverflow.com/questions/3191528/csv-in-python-adding-extra-carriage-return) – Eric Jul 05 '15 at 16:47
  • the code worked perfectly and in a much simpler way that the tools I was using, thank you! – sn3fru Aug 04 '15 at 12:25