在 csv 文件中添加新列并对记录进行操作
Add new column in a csv file and manipulate on the on records
我有 4 个名为 PV.csv、Dwel.csv、Sess.csv 和 Elap.csv 的 csv 文件。我在每个文件中有 15 列和 arouind 2000 行。起初我想在每个文件中添加一个名为 Var 的新列,并用相同的文件名填充新列的单元格。因此,PV.csv 文件中的新列 'Var' 将由 PV 填充。其他 3 个文件也是如此。
之后我想按如下方式操作所有文件。
最后我想合并/加入这 4 个基于 A_ID 和 B_ID 的文件,并将记录写入一个新的 csv 文件名 finalFile.csv。
感谢任何建议和帮助。
<p>PV.csv is as follows:</p>
A_ID B_ID LO UP LO UP
103 321 0 402
103 503 192 225 433 608
106 264 104 258 334 408
107 197 6 32 113 258
Dwell.csv如下:
A_ID B_ID LO UP LO UP
103 321 40 250 517 780
103 503 80 125 435 585
106 264 192 525 682
107 197 324 492 542 614
Session.csv如下:
A_ID B_ID LO UP LO UP
103 321 75 350 370 850
106 264 92 225 482 608
107 197 24 92 142
Elapsed.csv如下:
A_ID B_ID LO UP LO UP
103 321 5 35 75
103 503 100 225 333 408
106 264 102 325 582
107 197 24 92 142 214
PV.csv的第一个输出文件如下:
三个文件的所有其余部分将以相同的方式填充新列,其中包含 ehrer 文件名、Dwell、Session 和 Elapsed:
A_ID B_ID Var LO UP LO UP
103 321 PV 0 402
103 503 PV 192 225 433 608
106 264 PV 104 258 334 408
107 197 PV 6 32 113 258
最终输出文件如下:
finalFile.csv.
A_ID B_ID Var LO UP
103 321 PV 0 402
103 321 Dwel 40 250
103 321 Dwel 251 517
103 321 Dwel 518 780
103 321 Sess 75 350
103 321 Sess 351 370
103 321 Sess 371 850
103 321 Elap 5 35
103 321 Elap 36 75
103 503 PV 192 225
103 503 PV 226 433
103 503 PV 434 608
103 503 Dwel 80 125
103 503 Dwel 126 435
103 503 Dwel 436 585
103 503 Elap 100 225
103 503 Elap 226 333
103 503 Elap 334 408
106 264 PV 104 258
106 264 PV 259 334
106 264 PV 335 408
106 264 Dwel 192 525
106 264 Dwel 526 682
106 264 Sess 92 225
106 264 Sess 226 482
106 264 Sess 483 608
106 264 Elap 102 325
106 264 Elap 326 582
107 197 PV 6 32
107 192 PV 33 113
107 192 PV 114 258
107 192 Dwel 324 492
107 192 Dwel 493 542
107 192 Dwel 543 614
107 192 Sess 24 92
107 192 Sess 93 142
107 192 Elap 24 92
107 192 Elap 93 142
107 192 Elap 143 214
这些操作有一个标准库模块
https://docs.python.org/2/library/csv.html#module-csv
无论如何都不是完整的答案,但您的完整实施几乎肯定会从那里开始。上面的 python 文档包含几个可以帮助您入门的工作示例。
您应该使用 python 内置 csv 模块。
要创建最终的 csv 文件,您可以这样做。通读每个文件,将新的列值添加到每一行并将其写入新文件
import csv
with open('finalcsv.csv', 'w') as outcsv:
writer = csv.writer(outcsv)
writer.writerow(['a','b','c','etc','Var']) # write final headers
for filename in ['PV.csv','Dwel.csv','Sess.csv','Elap.csv']:
with open(filename) as incsv:
val = filename.split('.csv')[0]
reader = csv.reader(incsv) # create reader object
reader.next() # skip the headers
for row in reader:
writer.writerow(row+[val])
以下脚本应该可以帮助您入门:
from collections import defaultdict
from itertools import groupby
import csv
entries = defaultdict(list)
csv_files = [(0, 'PV.csv', 'PV'), (1, 'Dwell.csv', 'Dwel'), (2, 'Session.csv', 'Sess'), (3, 'Elapsed.csv', 'Elap')]
for index, filename, shortname in csv_files:
f_input = open(filename, 'rb')
csv_input = csv.reader(f_input)
header = next(csv_input)
for row in csv_input:
row[:] = [col for col in row if col]
entries[(row[0], row[1])].append((index, shortname, row[2:]))
f_input.close()
f_output = open('finalFile.csv', 'wb')
csv_output = csv.writer(f_output)
csv_output.writerow(header[:2] + ['Var'] + header[2:4])
for key in sorted(entries.keys()):
for k, g in groupby(sorted(entries[key]), key=lambda x: x[1]):
var_group = list(g)
if len(var_group[0][2]):
up = var_group[0][2][0]
for entry in var_group:
for pair in zip(*[iter(entry[2])]*2):
csv_output.writerow([key[0], key[1], entry[1], up, pair[1]])
up = int(pair[1]) + 1
f_output.close()
使用您提供的数据,得到以下输出:
A_ID,B_ID,Var,LO,UP
103,321,PV,0,402
103,321,Dwel,40,250
103,321,Dwel,251,780
103,321,Sess,75,350
103,321,Sess,351,850
103,321,Elap,5,35
103,503,PV,192,225
103,503,PV,226,608
103,503,Dwel,80,125
103,503,Dwel,126,585
103,503,Elap,100,225
103,503,Elap,226,408
106,264,PV,104,258
106,264,PV,259,408
106,264,Dwel,192,525
106,264,Sess,92,225
106,264,Sess,226,608
106,264,Elap,102,325
107,197,PV,6,32
107,197,PV,33,258
107,197,Dwel,324,492
107,197,Dwel,493,614
107,197,Sess,24,92
107,197,Elap,24,92
107,197,Elap,93,214
要处理文件夹中的所有 csv 文件,您可以将以下内容添加到脚本顶部:
import os
import glob
csv_files = [(index, file, os.path.splitext(file)[0]) for index, file in enumerate(glob.glob('*.csv'))]
您还应该更改输出文件的位置,否则它将在下次脚本 运行.
时被读取
使用 Python 2.6.6 进行测试(我相信这是 OP 使用的)
我有 4 个名为 PV.csv、Dwel.csv、Sess.csv 和 Elap.csv 的 csv 文件。我在每个文件中有 15 列和 arouind 2000 行。起初我想在每个文件中添加一个名为 Var 的新列,并用相同的文件名填充新列的单元格。因此,PV.csv 文件中的新列 'Var' 将由 PV 填充。其他 3 个文件也是如此。 之后我想按如下方式操作所有文件。
最后我想合并/加入这 4 个基于 A_ID 和 B_ID 的文件,并将记录写入一个新的 csv 文件名 finalFile.csv。 感谢任何建议和帮助。
<p>PV.csv is as follows:</p>
A_ID B_ID LO UP LO UP
103 321 0 402
103 503 192 225 433 608
106 264 104 258 334 408
107 197 6 32 113 258
Dwell.csv如下:
A_ID B_ID LO UP LO UP
103 321 40 250 517 780
103 503 80 125 435 585
106 264 192 525 682
107 197 324 492 542 614
Session.csv如下:
A_ID B_ID LO UP LO UP
103 321 75 350 370 850
106 264 92 225 482 608
107 197 24 92 142
Elapsed.csv如下:
A_ID B_ID LO UP LO UP
103 321 5 35 75
103 503 100 225 333 408
106 264 102 325 582
107 197 24 92 142 214
PV.csv的第一个输出文件如下:
三个文件的所有其余部分将以相同的方式填充新列,其中包含 ehrer 文件名、Dwell、Session 和 Elapsed:
A_ID B_ID Var LO UP LO UP
103 321 PV 0 402
103 503 PV 192 225 433 608
106 264 PV 104 258 334 408
107 197 PV 6 32 113 258
最终输出文件如下:
finalFile.csv.
A_ID B_ID Var LO UP
103 321 PV 0 402
103 321 Dwel 40 250
103 321 Dwel 251 517
103 321 Dwel 518 780
103 321 Sess 75 350
103 321 Sess 351 370
103 321 Sess 371 850
103 321 Elap 5 35
103 321 Elap 36 75
103 503 PV 192 225
103 503 PV 226 433
103 503 PV 434 608
103 503 Dwel 80 125
103 503 Dwel 126 435
103 503 Dwel 436 585
103 503 Elap 100 225
103 503 Elap 226 333
103 503 Elap 334 408
106 264 PV 104 258
106 264 PV 259 334
106 264 PV 335 408
106 264 Dwel 192 525
106 264 Dwel 526 682
106 264 Sess 92 225
106 264 Sess 226 482
106 264 Sess 483 608
106 264 Elap 102 325
106 264 Elap 326 582
107 197 PV 6 32
107 192 PV 33 113
107 192 PV 114 258
107 192 Dwel 324 492
107 192 Dwel 493 542
107 192 Dwel 543 614
107 192 Sess 24 92
107 192 Sess 93 142
107 192 Elap 24 92
107 192 Elap 93 142
107 192 Elap 143 214
这些操作有一个标准库模块 https://docs.python.org/2/library/csv.html#module-csv
无论如何都不是完整的答案,但您的完整实施几乎肯定会从那里开始。上面的 python 文档包含几个可以帮助您入门的工作示例。
您应该使用 python 内置 csv 模块。
要创建最终的 csv 文件,您可以这样做。通读每个文件,将新的列值添加到每一行并将其写入新文件
import csv
with open('finalcsv.csv', 'w') as outcsv:
writer = csv.writer(outcsv)
writer.writerow(['a','b','c','etc','Var']) # write final headers
for filename in ['PV.csv','Dwel.csv','Sess.csv','Elap.csv']:
with open(filename) as incsv:
val = filename.split('.csv')[0]
reader = csv.reader(incsv) # create reader object
reader.next() # skip the headers
for row in reader:
writer.writerow(row+[val])
以下脚本应该可以帮助您入门:
from collections import defaultdict
from itertools import groupby
import csv
entries = defaultdict(list)
csv_files = [(0, 'PV.csv', 'PV'), (1, 'Dwell.csv', 'Dwel'), (2, 'Session.csv', 'Sess'), (3, 'Elapsed.csv', 'Elap')]
for index, filename, shortname in csv_files:
f_input = open(filename, 'rb')
csv_input = csv.reader(f_input)
header = next(csv_input)
for row in csv_input:
row[:] = [col for col in row if col]
entries[(row[0], row[1])].append((index, shortname, row[2:]))
f_input.close()
f_output = open('finalFile.csv', 'wb')
csv_output = csv.writer(f_output)
csv_output.writerow(header[:2] + ['Var'] + header[2:4])
for key in sorted(entries.keys()):
for k, g in groupby(sorted(entries[key]), key=lambda x: x[1]):
var_group = list(g)
if len(var_group[0][2]):
up = var_group[0][2][0]
for entry in var_group:
for pair in zip(*[iter(entry[2])]*2):
csv_output.writerow([key[0], key[1], entry[1], up, pair[1]])
up = int(pair[1]) + 1
f_output.close()
使用您提供的数据,得到以下输出:
A_ID,B_ID,Var,LO,UP 103,321,PV,0,402 103,321,Dwel,40,250 103,321,Dwel,251,780 103,321,Sess,75,350 103,321,Sess,351,850 103,321,Elap,5,35 103,503,PV,192,225 103,503,PV,226,608 103,503,Dwel,80,125 103,503,Dwel,126,585 103,503,Elap,100,225 103,503,Elap,226,408 106,264,PV,104,258 106,264,PV,259,408 106,264,Dwel,192,525 106,264,Sess,92,225 106,264,Sess,226,608 106,264,Elap,102,325 107,197,PV,6,32 107,197,PV,33,258 107,197,Dwel,324,492 107,197,Dwel,493,614 107,197,Sess,24,92 107,197,Elap,24,92 107,197,Elap,93,214
要处理文件夹中的所有 csv 文件,您可以将以下内容添加到脚本顶部:
import os
import glob
csv_files = [(index, file, os.path.splitext(file)[0]) for index, file in enumerate(glob.glob('*.csv'))]
您还应该更改输出文件的位置,否则它将在下次脚本 运行.
时被读取使用 Python 2.6.6 进行测试(我相信这是 OP 使用的)