使用 itertools 处理 csv 中的数据 (python)
Manipulating data in csv using itertools (python)
我正在尝试向下面的代码添加一个功能,但似乎在某处搞砸了。
以下代码基本上为以下每个说话者重复前 "z" table 列(如 excel 换位)。 "z" 之后的这些列是日期,只需要一个日期列。
问题是python脚本正确生成了值,但需要它插入谈论那个日期的列,重复这个信息到那个日期的所有值。
Python 脚本:
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 ()
东西(输入):
[[X; y; date1; date2; date3]
[01; 02; 03; 04; 05]
[06; 07; 08; 09; 10]
[11; 12; 13, 14 15]]
当前脚本正确执行此操作(现在输出):
[[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]]
我的输出是这样的(期望的输出):
[[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]]
有人做过这样的事吗?
您有什么理由不能只使用 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()
我正在尝试向下面的代码添加一个功能,但似乎在某处搞砸了。
以下代码基本上为以下每个说话者重复前 "z" table 列(如 excel 换位)。 "z" 之后的这些列是日期,只需要一个日期列。
问题是python脚本正确生成了值,但需要它插入谈论那个日期的列,重复这个信息到那个日期的所有值。
Python 脚本:
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 ()
东西(输入):
[[X; y; date1; date2; date3]
[01; 02; 03; 04; 05]
[06; 07; 08; 09; 10]
[11; 12; 13, 14 15]]
当前脚本正确执行此操作(现在输出):
[[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]]
我的输出是这样的(期望的输出):
[[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]]
有人做过这样的事吗?
您有什么理由不能只使用 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()