values in csv

549
2
04-30-2014 08:57 AM
benberman
Occasional Contributor
I have a csv that looks like this:

A,B,C,D
(Newline)
2,3,4,feature_1
1,2,4,feature_1
#Average line "","",4
(Newline)
3,6,7,feature_2
1,2,6,feature_2
#Average line "","",""6.5

I want to be able to take the rows for each "feature" and write another line that takes the average of the values in column "C". I have inserted comments for your convenience.
Tags (2)
0 Kudos
2 Replies
Zeke
by
Regular Contributor III
You want something like the readline method. Here is the basics, not a full version, but you get the idea...


with open("foo.csv", "r") as f:
    f.readline()    # get rid of the first line
    
    # this section goes in a loop
    f.readline()    #discard newline
    lineOne = f.readline()    # first line with a value
    x = float(lineOne.split(",")[2])
    lineTwo = f.readline()
    x += (float(lineTwo.split(",")[2]))/2   # this is your value
    # do something

0 Kudos
JoshuaChisholm
Occasional Contributor III
Hello FLBB,

Along the same train of thoughts as Greg, this might work:
infile=open(r'C:\Path\To\Input.csv','r') #opens a read only copy

#get csv into array:
lines=[]
for line in infile:
    lines.append(line.split(','))
infile.close()

toOut=[lines[0]] #start output with the header
prevFC=lines[1][3] #start with first fc
tempList=[]

for line in lines[1:]: #skip header
    if line[0]=='\n' or line[0]=="(Newline)\n":
        continue
    fc=line[3]
    if prevFC!=fc:
        avg=sum(tempList)/float(len(tempList))
        toOut.append(['','',str(avg),'\n'])
        toOut.append(['','','','\n']) #add blank line
        tempList=[]
    toOut.append(line)
    val=float(line[2])
    tempList.append(val)
    prevFC=fc

#add last average line:
avg=sum(tempList)/float(len(tempList))
toOut.append(['','',str(avg),'\n'])
toOut.append(['','','','\n']) #add blank line

#build output:
toOutCSV=''
for line in toOut:
    toOutCSV+=','.join(line)

#Write to output file:
outfile=open(r'C:\Path\To\Output.csv','w') #opens a write copy (will overwrite output)
outfile.write(toOutCSV)
outfile.close()

0 Kudos