简体   繁体   中英

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. 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.

Do keep in mind that CSV's are structured in a tabular fashion (like 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. 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:

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).

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:

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.

You can also use base python functions to write csv file.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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