I have a CSV file which looks like below. Right now it doesn't have any columns and it contains some fields I actually don't need so I need to write it into a new file based on some conditions.
!PROJECT1, OBJECT1
2020-09-10+02:00,100,HHH,SAS,RM$20,1,1
2020-09-16+02:00,200,GGG,SAS,TAKE,2020-09-16+02:00
2020-09-13+02:00,300,TTT,SAS,TAKE,2020-09-13+02:00
2020-09-11+02:00,100,HHH,SAS,RM$20,1,1
These are the conditions:
- I will only write the record if
index[4]
contains the wordTAKE
. If so, takeindex[0]
,[4]
and[5]
. - Index[0] and [5] need to be spitted and named in
YEAR
,MONTH
andDAY
andTD
.index[4]
needs to be namedTYPE
I want my new file to look like this:
YEAR MONTH DAY TD TYPE YEAR MONTH DAY TD
2020 09 16 2 TAKE 2020 09 16 2
2020 09 13 2 TAKE 2020 09 13 2
This is my code:
def filter_row(r):
condition_1 = r[4] == 'TAKE' #<-- take only the TAKE's
with open(file_path, 'r') as my_file, open('outputfile.txt', 'w') as outer:
reader = csv.reader(my_file, delimiter = ',')
next(reader) #Skip the first row because it's just the header
writer = csv.writer(outer, delimiter = '\t')
for row in reader:
if filter_row(row):
writer.writerow(row)
Right now my output file looks like this:
2020-09-16+02:00, 200, GGG, SAS, TAKE, 2020-09-16+02:00
2020-09-13+02:00, 300, TTT, SAS, TAKE, 2020-09-13+02:00