简体   繁体   English

如何将列表的一部分写入CSV?

[英]How to write part of a list to a csv?

I have data in these lists. 我在这些列表中有数据。 I need to use these certain elements at the beginning of the row, and then add 30 data points after. 我需要在行的开头使用这些特定元素,然后在其后添加30个数据点。 I understand how to splice a list, but I want to return those individual items from the list 我知道如何拼接列表,但是我想从列表中返回那些单独的项目

w.writerow([sub_sub_header_list[0], data_list[0:29]])
w.writerow([sub_sub_header_list[1], data_list[30:59]])
w.writerow([sub_sub_header_list[2], data_list[60:89]])
w.writerow([sub_sub_header_list[3], data_list[90:119]])

I get something like this: 我得到这样的东西:

Team Stats, [u'310', u'5291', u'1018', u'5.2', u'27', u'11', u'289', u'377', u'598', u'3879', u'26', u'16', u'6.3', u'190', u'398', u'1412', u'6', u'3.5', u'73', u'88', u'857', u'26', u'193', u'27.5', u'13.0', u'Own 27.6', u'2:21', u'5.40', u'27.4']

When I want: 当我想要的时候:

Team Stats, [310, 5291, 1018 ,...] and so forth. Team Stats, [310, 5291, 1018 ,...]等。

Do keep in mind that CSV's are structured in a tabular fashion (like excel). 请记住,CSV是以表格形式组织的(例如excel)。 You have the header first, then the data for each column of the header on separate rows. 首先有标题,然后是标题的每一列的数据在单独的行上。 When you do a writerow you must provide it with the actual values, for specific columns, for the current row that's being written. 执行writerow ,必须为它提供正在写入的当前行的特定列的实际值。 You've basically written a couple of lists in your CSV on a each column by doing w.writerow([sub_sub_header_list[0], data_list[0:29]]) which is essentially w.writerow([1, 2...], [3, 4,...]) , that's why you got in the CSV data like: 通过执行w.writerow([sub_sub_header_list[0], data_list[0:29]])这基本上是w.writerow([1, 2...], [3, 4,...]) ,这就是为什么要输入CSV数据,例如:

u'[1,2,..]', u'[3,4,...]'

It was basically treating each list as an individual cell, and converting it to string so that it can store it in the CSV (that's where the u'' comes from). 基本上是将每个列表视为一个单独的单元格,然后将其转换为字符串,以便可以将其存储在CSV中(这就是u''来源)。

You basically have to keep a referenced index throughout the vector, since it's a 1-dimensional data structure that has the series appended one after another. 从根本上说,您必须在整个向量中保留一个引用索引,因为它是一维数据结构,序列之间是一个接一个地附加的。

import csv

pf = open("out.csv", "w")

csv_writer = csv.DictWriter(pf, fieldnames=["A", "B", "C"])
csv_writer.writeheader()

LENGTH = 3  # number elements per column
data_list = [1, 1, 2, 2, 3, 3]

for i in range(LENGTH):
    csv_writer.writerow({
        'A': data_list[i],
        'B': data_list[i+LENGTH],
        'C': data_list[i+LENGTH*2],
    })

pf.close()

and the output would be something like: 输出将是这样的:

A,B,C
1,2,3
1,2,3

If I deciphered your question correctly, something like this would do it: 如果我正确地理解了您的问题,则可以执行以下操作:

import csv
from itertools import zip_longest


def grouper(n, iterable, sentinel=object()):
    """ Collect data into fixed-length chunks or blocks. """
    args = [iter(iterable)] * n
    for t in zip_longest(*args, fillvalue=sentinel):
        yield list(elem for elem in t if elem is not sentinel)


# Example usage.
data_list = [u'310', u'5291', u'1018', u'5.2', u'27', u'11', u'289', u'377', u'598',
             u'3879', u'26', u'16', u'6.3', u'190', u'398', u'1412', u'6', u'3.5', u'73',
             u'88', u'857', u'26', u'193', u'27.5', u'13.0', u'Own 27.6', u'2:21',
             u'5.40', u'27.4']
sub_sub_header_list = [u'sub_header_0', u'sub_header_1', u'sub_header_2',
                       u'sub_header_3']

output_filename = 'grouped_data.csv'
group_size = len(sub_sub_header_list)
with open(output_filename, 'w', newline='') as csv_file:
    csv_writer = csv.writer(csv_file)
    for i, group in enumerate(grouper(group_size, data_list)):
        row = [sub_sub_header_list[i%group_size]] + group
        csv_writer.writerow(row)

print('File {!r} written.'.format(output_filename))

Here's the contents of the csv file it created given the sample data: 给定示例数据,这是它创建的csv文件的内容:

sub_header_0,310,5291,1018,5.2
sub_header_1,27,11,289,377
sub_header_2,598,3879,26,16
sub_header_3,6.3,190,398,1412
sub_header_0,6,3.5,73,88
sub_header_1,857,26,193,27.5
sub_header_2,13.0,Own 27.6,2:21,5.40
sub_header_3,27.4

Note that since the number of items in the data_list wasn't an exact multiple of the number in the sub_sub_header_list , the last row isn't as long as the ones before it. 请注意,由于data_list的项目数不是data_list的项目数的精确倍数,因此最后一行的sub_sub_header_list前一行的长度。

You can also use base python functions to write csv file. 您还可以使用基本的python函数编写csv文件。

Lets say your data is as follows: 可以说您的数据如下:

# elements converted to strings:
data_list = list(map(str, numpy.random.randint(1,100,20)))
sub_sub_header_list = ['A','B','C']

Following code will produce desired list: 以下代码将产生所需的列表:

rowsize = len(sub_sub_header_list)
outlist=[]
# create header:
outlist.append(",".join(sub_sub_header_list))         
# create rows:
for i in range(0,len(data_list)-rowsize,rowsize):
    outlist.append(",".join(data_list[i:i+rowsize]))  
# show format:
print("\n".join(outlist))            

Output is in the needed format: 输出为所需格式:

A,B,C
16,72,38
79,4,37
93,19,77
87,54,87
26,4,17
73,59,56

And csv file can be produced by: 并可以通过以下方式生成csv文件:

with open("outfile.csv", "w") as f:     
    f.write("\n".join(outlist))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM