简体   繁体   中英

Extract designated data from one csv file then assign to another csv file using python

I got a csv file containing data in this form,

在此处输入图片说明

I want to extract data from column C and write them into a new csv file, like this,

在此处输入图片说明

So I need to do 2 things:

  • write 'node' and number from 1 to 22 into the first row and column (since in this case, there are 22 in one repeated cycle in the column A in input csv)

在此处输入图片说明

  • I have got data in column c extracted and write in output csv, like this,

在此处输入图片说明

I need to transpose those data every 22 rows one time and fill them in row starts from B2 position in excel, then B3, B4,...etc.

It's clear that I must loop through every row to do this efficiently, but I don't know how to apply the csv module in python.

Should I download the xlrd package, or can I handle this only use the built-in csv module?

I am working with python 2.7.6 and pyscripter under Windows 8.1 x64. Feel free to give me any suggestion, thanks a lot!

Read the csv python documentation .

The simple way to iterate through rows with csv reader:

import csv

X = []
spamreader = csv.reader('path_to_file/filename.csv',delimiter=',')
for row in spamreader:
    X.append(row)

This creates a variable with all the csv data. The structure of your file will make it difficult to read because the cell_separator is ',' but there are also multiple commas within each cell and because of the parentheses there will be a mixture of string and numerical data that will require some cleaning. If you have access to reformatting the csv it might be easier if each cell looked like 1,2,0.01 instead of (1,2,0.01), also consider using a different delimiter between cells such as ';'.

If not expect some tedious data cleaning, and definitely read through the documentation linked above.

Edit: Try the following

import csv
X = [] 
with open('path_to_file/filename.csv','rb') as csvfile:
    spamreader = csv.reader(csvfile,delimiter=',')
    for row in spamreader:
        rowTemp = []
        for i in range(len(row)):
            if (i+1)%3==0:  #gets every third cell
                rowTemp.append(row[i])
        X.append(rowTemp)

This is a matrix of all the distance values. Then try:

with open('path_to_output_file/output_file.csv','wb') as csvfile:
spamwriter = csv.writer(csvfile,delimter=',')
for sublist in X:
    spamwriter.writerow(sublist)

Not sure if this is exactly what you're looking for but it should be close. It ouputs a csv file that is stripped of all the node pairs

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