I’m working on translating some php code to Python. I’m not an expert in either. This may be a lot, but a little bit of would be appreciated.
I’m especially struggling with this chunk of the code. Here, we have an identifer identifer, namely row[0]row[1]row[2]. Row[4] is a timestamp. For each identifier, we want to find the ‘start’ and ‘end’ dates — named as you would think. This is how a colleague wrote the code to do so: I’ve added the comments as I understand what is happening but not how to translate this to Python.
So far in Python I’ve been able to create a dictionary of identifier : timestamp, but have not been able to successfully use that to my advantage in this part of the problem.
Questions:
– Am I missing something very obvious and helpful about Python in this instance?
– Anywhere I can find an easy to understand mapping of PHP to Python rules (if it exists)?
fgetcsv($inputhandle); //read input as csv
while ($row = fgetcsv($inputhandle)) { //read each line in record
$date_arr = date_parse_from_format("Y-m-d H:i:s",$row[4]); //parse timestamp
$newdate = mktime($date_arr['hour'],
$date_arr['minute'],
$date_arr['second'],
$date_arr['month'],
$date_arr['day'],
$date_arr['year']); //store each piece in array form
$endpoints[$row[0]][$row[1]][$row[2]]['end'] = $newdate; //identify endpoint
if(!isset($records[$row[0]][$row[1]][$row[2]])) {
$endpoints[$row[0]][$row[1]][$row[2]]['start'] = $newdate; //identify startpoint
}
$records[$row[0]][$row[1]][$row[2]][$newdate]=$row[5]; //row 5 holds values we care about
}
For context, here is the process that I’m trying to replicate:
identify endpoints of time<br>
starttime<br>
endtime<br>
create 15 minute time vector while : startime < time < endtime<br>
Here’s what I have so far in Python:
import sys
import csv
from datetime
import time
import numpy
import itertools
//open file with csv reader
filename = open('ICUdata.csv', 'rb')
d = {} // declare dictionary variable
//read file as csv, parse
spam = csv.reader(filename, delimiter=',')
next(spam) #skip header
for row in spam: // first fill in missing values
if row[1] == '':
row[1] = '99999'
if row[2] == '':
row[2] = '99999'
//need datetime parse
datestring = row[4]
date = time.strptime(datestring, "%Y-%m-%d %H:%M:%S") // parsed datetime
//create dictionary of identifiers : realtime
k = id #need this instead of just row[0:3] which creates list
v = row[4]
if k not in d:
d[k] = [v]
#this = row[0:3].append(endpoint)
else:
d[k].append(v)
Something that may be of use to you is the
struct_timein package time that is returned by thetime.strptime(string[, format]). It is a named tuple.A little code to demonstrate:
which produces the output:
from which the individual elements can be recovered via:
which outputs
Edit: I rushed through this when I saw the date bits. Surprisingly, nobody else has answered yet so I’ll try to sweeten the pot for you a bit.
I personally would have probably just done file.readline() and re.split() for csv’s because I have a tendency to be a DIY’er (or NIH’er, depending on how you want to color that one…)
Through a bit of research though, I’ve just learned that python specifically has a csv module for handling such situations. It’s been around since 2.3 and is surely more robust than doing it yourself. Go figure.
Sources for your perusal:
Python time module
Python csv module