Python 将列表写入CSV文件,并在满足条件时启动新列
我有一个包含数据点和“标识符”的列表,如下所示:Python 将列表写入CSV文件,并在满足条件时启动新列,python,csv,Python,Csv,我有一个包含数据点和“标识符”的列表,如下所示: ['identifier', 1, 2, 3, 4, 'identifier', 10, 11, 12, 13, 'identifier', ...] 我想将此列表写入CSV文件,并为每个标识符创建一个新列。 e、 g 我期待着听到你的建议 干杯 -Sebastian你可以这样做,假设l是你的列表: import pandas as pd import numpy as np pd.DataFrame(np.array(l).reshape(-
['identifier', 1, 2, 3, 4, 'identifier', 10, 11, 12, 13, 'identifier', ...]
我想将此列表写入CSV文件,并为每个标识符创建一个新列。
e、 g
我期待着听到你的建议
干杯
-Sebastian你可以这样做,假设
l
是你的列表:
import pandas as pd
import numpy as np
pd.DataFrame(np.array(l).reshape(-1,5)).set_index(0).T.to_csv('my_file.csv',index=0)
如果您的数据集不是太大,您应该首先准备数据,然后将其序列化为csv文件
import csv
dataset = ['identifier', 1, 2, 3, 4, 'identifier', 10, 11, 12, 13, 'identifier', 21, 22, 23, 24]
columns = []
col = []
for datapoint in dataset:
if datapoint == 'identifier':
if col:
columns.append(col)
col = []
else:
col.append(datapoint)
columns.append(col)
rows_count = max((len(c) for c in columns))
with open('result.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=";")
for x in range(rows_count):
data = []
for col in columns:
if len(col) > x:
data.append(col[x])
else:
data.append("")
writer.writerow(data)
此解决方案不会将数据写入csv文件,但使用csv库这是一个简单的步骤。这样做的目的是将您提供的数据重新构造为一个列表列表,每个子列表都是一行数据
l=['identifier',1,2,3',identifier',10,11,12,13',identifier',4,3,2,1,10]
def拆分列表(左,开):
“”“将列表拆分为标识符,并返回在上拆分的列表列表。”
标识符,但不包括它。”“”
拆分=[]
缓存=[]
对于l中的v:
#检查这是否是一个标识符
如果v==开启:
#除非缓存为空,否则将其添加到拆分
如果缓存:
拆分.追加(缓存)
#清空缓存
缓存=[]
其他:
cache.append(v)
#如果不是empyt,则将最后一个缓存添加到拆分
如果缓存:
拆分.追加(缓存)
返回拆分
def重塑列表(l,默认值=无):
“”“获取一个列表列表,假设每个列表都是一列值和
如果列表的长度不完全相同,则将其重塑为行列表无
将用于填充empyt点。”“”
结果=[]
#获取最长列表的长度
maxlen=max(映射(len,l))
对于范围内的i(最大值):
#创建每一行
行=[]
#从列中提取值
对于l中的列:
如果i
生成演示数据:
import random
random.seed(20180119) # remove to get random data between runs
id = 'identifier'
def genData():
data = []
for n in range(10+random.randint(1,10)):
data.append(id)
data.extend(random.choices(range(1,20),k=random.randint(3,12)))
print(data)
return data
def partitionData(idToUse,dataToUse):
lastId = None
for (i,n) in enumerate(data): # identify subslices of data
if n == idToUse and not lastId: # find first id, data before is discarded
lastId = i
continue
if n == idToUse: # found id
yield data[lastId:i] # yield sublist including idToUse
lastId = i
if (data[-1] != id): # yield rest of data
yield data[lastId:]
data = genData()
partitioned = partitionData(id, data)
import itertools
import csv
with open('result.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=";")
# like zip, but fills up shorter ones with None till longest index
writer.writerows(itertools.zip_longest(*partitioned, fillvalue=None))
identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier
10;6;3;6;17;18;13;15;18;15;17;9;3;15
17;10;7;17;8;6;7;8;16;2;19;17;16;12
17;1;7;8;3;18;4;17;10;18;15;18;15;2
10;14;4;8;8;19;13;8;7;13;4;8;13;16
15;4;8;13;2;7;;1;16;7;18;17;9;2
12;;2;15;19;8;;12;18;;7;17;;5
16;;16;7;16;14;;16;19;;13;17;;16
18;;8;9;2;7;;7;6;;17;;;18
19;;1;4;5;7;;5;15;;8;;;
18;;8;10;6;19;;19;8;;9;;;
14;;16;15;;;;14;13;;;;;
9;;6;;;;;9;15;;;;;
输出:
['identifier', 18, 6, 19, 10, 12, 18, 17, 12,
'identifier', 10, 17, 17, 10, 15, 12, 16, 18, 19, 18, 14, 9,
'identifier', 6, 10, 1, 14, 4,
'identifier', 3, 7, 7, 4, 8, 2, 16, 8, 1, 8, 16, 6,
'identifier', 6, 17, 8, 8, 13, 15, 7, 9, 4, 10, 15,
'identifier', 17, 8, 3, 8, 2, 19, 16, 2, 5, 6,
'identifier', 18, 6, 18, 19, 7, 8, 14, 7, 7, 19,
'identifier', 13, 7, 4, 13,
'identifier', 15, 8, 17, 8, 1, 12, 16, 7, 5, 19, 14, 9,
'identifier', 18, 16, 10, 7, 16, 18, 19, 6, 15, 8, 13, 15,
'identifier', 15, 2, 18, 13, 7,
'identifier', 17, 19, 15, 4, 18, 7, 13, 17, 8, 9,
'identifier', 9, 17, 18, 8, 17, 17, 17,
'identifier', 3, 16, 15, 13, 9,
'identifier', 15, 12, 2, 16, 2, 5, 16, 18]
重新格式化:
import random
random.seed(20180119) # remove to get random data between runs
id = 'identifier'
def genData():
data = []
for n in range(10+random.randint(1,10)):
data.append(id)
data.extend(random.choices(range(1,20),k=random.randint(3,12)))
print(data)
return data
def partitionData(idToUse,dataToUse):
lastId = None
for (i,n) in enumerate(data): # identify subslices of data
if n == idToUse and not lastId: # find first id, data before is discarded
lastId = i
continue
if n == idToUse: # found id
yield data[lastId:i] # yield sublist including idToUse
lastId = i
if (data[-1] != id): # yield rest of data
yield data[lastId:]
data = genData()
partitioned = partitionData(id, data)
import itertools
import csv
with open('result.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=";")
# like zip, but fills up shorter ones with None till longest index
writer.writerows(itertools.zip_longest(*partitioned, fillvalue=None))
identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier
10;6;3;6;17;18;13;15;18;15;17;9;3;15
17;10;7;17;8;6;7;8;16;2;19;17;16;12
17;1;7;8;3;18;4;17;10;18;15;18;15;2
10;14;4;8;8;19;13;8;7;13;4;8;13;16
15;4;8;13;2;7;;1;16;7;18;17;9;2
12;;2;15;19;8;;12;18;;7;17;;5
16;;16;7;16;14;;16;19;;13;17;;16
18;;8;9;2;7;;7;6;;17;;;18
19;;1;4;5;7;;5;15;;8;;;
18;;8;10;6;19;;19;8;;9;;;
14;;16;15;;;;14;13;;;;;
9;;6;;;;;9;15;;;;;
写入数据:
import random
random.seed(20180119) # remove to get random data between runs
id = 'identifier'
def genData():
data = []
for n in range(10+random.randint(1,10)):
data.append(id)
data.extend(random.choices(range(1,20),k=random.randint(3,12)))
print(data)
return data
def partitionData(idToUse,dataToUse):
lastId = None
for (i,n) in enumerate(data): # identify subslices of data
if n == idToUse and not lastId: # find first id, data before is discarded
lastId = i
continue
if n == idToUse: # found id
yield data[lastId:i] # yield sublist including idToUse
lastId = i
if (data[-1] != id): # yield rest of data
yield data[lastId:]
data = genData()
partitioned = partitionData(id, data)
import itertools
import csv
with open('result.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=";")
# like zip, but fills up shorter ones with None till longest index
writer.writerows(itertools.zip_longest(*partitioned, fillvalue=None))
identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier
10;6;3;6;17;18;13;15;18;15;17;9;3;15
17;10;7;17;8;6;7;8;16;2;19;17;16;12
17;1;7;8;3;18;4;17;10;18;15;18;15;2
10;14;4;8;8;19;13;8;7;13;4;8;13;16
15;4;8;13;2;7;;1;16;7;18;17;9;2
12;;2;15;19;8;;12;18;;7;17;;5
16;;16;7;16;14;;16;19;;13;17;;16
18;;8;9;2;7;;7;6;;17;;;18
19;;1;4;5;7;;5;15;;8;;;
18;;8;10;6;19;;19;8;;9;;;
14;;16;15;;;;14;13;;;;;
9;;6;;;;;9;15;;;;;
result.csv:
import random
random.seed(20180119) # remove to get random data between runs
id = 'identifier'
def genData():
data = []
for n in range(10+random.randint(1,10)):
data.append(id)
data.extend(random.choices(range(1,20),k=random.randint(3,12)))
print(data)
return data
def partitionData(idToUse,dataToUse):
lastId = None
for (i,n) in enumerate(data): # identify subslices of data
if n == idToUse and not lastId: # find first id, data before is discarded
lastId = i
continue
if n == idToUse: # found id
yield data[lastId:i] # yield sublist including idToUse
lastId = i
if (data[-1] != id): # yield rest of data
yield data[lastId:]
data = genData()
partitioned = partitionData(id, data)
import itertools
import csv
with open('result.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=";")
# like zip, but fills up shorter ones with None till longest index
writer.writerows(itertools.zip_longest(*partitioned, fillvalue=None))
identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier;identifier
10;6;3;6;17;18;13;15;18;15;17;9;3;15
17;10;7;17;8;6;7;8;16;2;19;17;16;12
17;1;7;8;3;18;4;17;10;18;15;18;15;2
10;14;4;8;8;19;13;8;7;13;4;8;13;16
15;4;8;13;2;7;;1;16;7;18;17;9;2
12;;2;15;19;8;;12;18;;7;17;;5
16;;16;7;16;14;;16;19;;13;17;;16
18;;8;9;2;7;;7;6;;17;;;18
19;;1;4;5;7;;5;15;;8;;;
18;;8;10;6;19;;19;8;;9;;;
14;;16;15;;;;14;13;;;;;
9;;6;;;;;9;15;;;;;
链接:-
-我期待着看到您的尝试。在这个网站上已经有数千个关于读/写CSV的问题。你从你的研究中尝试了什么?嗨,我尝试了大部分。我缺少的元素是告诉作者,如果符合条件,就开始新专栏。请将您的最佳尝试作为问题的编辑。如果我们在回答问题时也能纠正您的误解,那么对您来说就更有用了。您可能还会想,为什么一个“标识符”后面跟着4个值,以及为什么它们应该进入新的列-您从不谈论行…@pault是的,您可以。
writerows
方法获取一个嵌套列表,每个内部列表表示一行(该列表中的每个项目位于单独的列中)。您可以很容易地将此输入分解为行和列,作为带有for
循环的嵌套列表,并可能将其压缩为列表理解。这些问题没有明确的答案,因为还不清楚。你提供的任何答案都将基于你对Q想要什么的假设,而不是他在QT中陈述的意图。这是我对问题的理解。现场直播,谢谢:)我得出了与你相同的结论(格式方面),但直到他陈述他想让答案“思考”是正确的,但在大多数情况下,Q的变化是这样的。现在问题更明确了,你的解决方案中断了:)■