We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,956 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

python csv file

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

3
Contributors
2
Replies
1 Day
Discussion Span
2 Months Ago
Last Updated
46
Views
ajit.nayak3
Newbie Poster
3 posts since Feb 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This post shows how to read a csv file.

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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
'''    
bumsfeld
Nearly a Posting Virtuoso
1,495 posts since Jul 2005
Reputation Points: 409
Solved Threads: 235
Skill Endorsements: 1

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.2912 seconds using 2.68MB