1.11M Members

python csv file

 
0
 

Dear all ,

i have an csv file . I wanna copy content particular column and paste in another csv file.where i am trying to calcultion and plot graph

assume original csv file contain i.e column 4 contain time(hh.mm.ss) format continue by other columns
i wanna copy these data to another csv file.i wanna calculate TS=(3600hour)+(60min)+sec and plot graph for TS

how can do it in python code

 
0
 

This post shows how to read a csv file.

 
0
 

Here is one well commented example:

''' data4_expand.py
data4.csv looks like that:
date,time,v1,v2
03/13/2013,11.23.10,12.3,3.4
03/13/2013,11.23.13,12.7,5.1
03/13/2013,11.23.16,15.1,8.3
03/13/2013,11.23.19,17.8,9.8
03/13/2013,11.23.22,19.6,11.4
03/13/2013,11.23.25,21.3,14.6
03/13/2013,11.23.28,24.2,18.3
'''

# read the csv file and process
with open("data4.csv") as fin:
    data_list = []
    for ix, line in enumerate(fin):
        # strip off trailing new line char
        line = line.rstrip()
        # split at the separator char
        tlist = line.split(',')
        date, time, v1, v2 = tlist
        # treat title line differently
        if ix > 0:
            hr, mn, sc = time.split('.')
            seconds = float(hr)*3600 + float(mn)*60 + float(sc)            
            data_list.append([date, time, v1, v2, str(seconds)])
        else:
            # title line
            tlist.append('seconds')
            data_list.append(tlist)


# write out the expanded csv file
with open("data5.csv", "w") as fout:
    # format data string
    data_str = ""
    for line in data_list:
        print ",".join(line)  # test
        # use appropriate separator char
        data_str += ",".join(line) + '\n'
    fout.write(data_str)

print('-'*40)
print("Data file {} has been written".format("data5.csv"))

''' test result >>>
date,time,v1,v2,seconds
03/13/2013,11.23.10,12.3,3.4,40990.0
03/13/2013,11.23.13,12.7,5.1,40993.0
03/13/2013,11.23.16,15.1,8.3,40996.0
03/13/2013,11.23.19,17.8,9.8,40999.0
03/13/2013,11.23.22,19.6,11.4,41002.0
03/13/2013,11.23.25,21.3,14.6,41005.0
03/13/2013,11.23.28,24.2,18.3,41008.0
----------------------------------------
Data file data5.csv has been written
'''    
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article