I need to do CSV parsing and I was wondering if this is possible with python and where I should go to learn how to code this script or get help.


Let's say I have a CSV file with 20 rows and 4 columns. I need to:
-Strip out the first five rows
-Strip out the last three rows
-Strip out the 2nd and 4th columns
-Convert the dates in the 3rd column from format DD-MM-YYYY to MMM-DD (so for example, a 14-05-1999 gets converted to May-14)

In the end, I'll have a CSV file with 12 rows and 2 columns, with one of the columns in a different date format.

If this is in fact possible, can someone please point me to some tutorials that would help me achieve this in the least time possible? THANKS SO MUCH!

Recommended Answers

Formatting time/date strings is easy with Python module time ...
[code]# convert 14-05-1999 to May-14

see Python manual for module time format specifiers

import time

date_old = "14-05-1999"

use strptime(time_str, format_str) to form a time tuple

time_tuple = time.strptime(date_old, "%d-%m-%Y")

print(time_tuple) # test
use time.strftime(format_str, time_tuple) …
Jump to Post

All 4 Replies

Re: Is this even possible? 80 80

Python has a CSV reader/writer http://docs.python.org/release/2.5.2/lib/csv-examples.html. I would suggest you start with reading the file and printing individual rows and columns so you discover how to access them individually. Google is your friend here.

Re: Is this even possible? 80 80

strip out == do not write back again, when you write changed file with old name (maybe destroying the content in case of program bug) or new name with open('myoutfile.txt','w').write(my_changed_text)

Re: Is this even possible? 80 80

Formatting time/date strings is easy with Python module time ...

# convert 14-05-1999 to May-14
# see Python manual for module time format specifiers

import time

date_old = "14-05-1999"

# use strptime(time_str, format_str) to form a time tuple
time_tuple = time.strptime(date_old, "%d-%m-%Y")

#print(time_tuple)  # test

# use time.strftime(format_str, time_tuple) to create new format
date_new = time.strftime("%b-%d", time_tuple)

print(date_old)  # 14-05-1999
print(date_new)  # May-14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.