Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7419291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:02:31+00:00 2026-05-29T08:02:31+00:00

I’m working on translating some php code to Python. I’m not an expert in

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:02:32+00:00Added an answer on May 29, 2026 at 8:02 am

    Something that may be of use to you is the struct_time in package time that is returned by the time.strptime(string[, format]). It is a named tuple.

    A little code to demonstrate:

    import time
    fifth_element_of_row = "2000-12-31 23:11:12"
    my_time_struct = time.strptime(fifth_element_of_row, "%Y-%m-%d %H:%M:%S")
    print my_time_struct
    

    which produces the output:

    time.struct_time(tm_year=2000, tm_mon=12, 
    tm_mday=31, tm_hour=23, tm_min=11, tm_sec=12, 
    tm_wday=6, tm_yday=366, tm_isdst=-1)
    

    from which the individual elements can be recovered via:

    print my_time_struct.tm_year
    

    which outputs

    2000
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.