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

  • Home
  • SEARCH
  • 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 1838340
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:27:54+00:00 2026-05-17T06:27:54+00:00

I’m pretty new to Python, so hopefully the problem I’m having has a simple

  • 0

I’m pretty new to Python, so hopefully the problem I’m having has a simple solution.

At work we always us either Shell (ksh) or Perl for all of our scripting work. Since python has been shipped with Solaris for some time now, it has (finally) been given the green light as a scripting platform. I’ve started prototyping some improvements to our scripts using Python.

What I’m trying to accomplish is taking a time stamp and a string representing a time stamp and creating a datetime object for some date arithmetic.

My example code follows:

#!/bin/python

import datetime

fileTime="201009211100"
format = "YYYYmmdd"

yIdxS = format.find('Y')
yIdxE = format.rfind('Y')

if not fileTime[yIdxS:yIdxE+1].isdigit():
    print "ERROR: Year in wrong format"
    exit
else:
    print "Year [" + fileTime[yIdxS:yIdxE+1] + "]"

mIdxS = format.find('m')
mIdxE = format.rfind('m')

if not fileTime[mIdxS:mIdxE+1].isdigit():
    print "ERROR: Month in wrong format"
    exit
else:
    print "Month [" + fileTime[mIdxS:mIdxE+1] + "]"

dIdxS = format.find('d')
dIdxE = format.rfind('d')

if not fileTime[dIdxS:dIdxE+1].isdigit():
    print "ERROR: Day in wrong format"
    exit
else:
    print "Day [" + fileTime[dIdxS:dIdxE+1] + "]"


old = datetime.date( fileTime[yIdxS:yIdxE+1], \
                     fileTime[mIdxS:mIdxE+1], \
                     fileTime[dIdxS:dIdxE+1] );

I’m getting the following output/error:

Year [2010]
Month [09]
Day [21]
Traceback (most recent call last):
  File "./example.py", line 37, in <module>
    fileTime[dIdxS:dIdxE+1] );
TypeError: an integer is required

I don’t understand why I’m getting this TypeError exception. My understanding of Python’s dynamic typing is that I shouldn’t need to convert a string to an integer if the string is all digits.

So the problem would seem to be I’m either missing something that I need, or my understanding of the language is flawed.

Any help would be greatly appreciated. Thanks.

  • 1 1 Answer
  • 1 View
  • 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-17T06:27:54+00:00Added an answer on May 17, 2026 at 6:27 am

    Strongly consider using datetime.datetime.strptime:

    import datetime
    
    tests=["201009211100","201009211199"]
    for fileTime in tests:
        try:
            date=datetime.datetime.strptime(fileTime,'%Y%m%d%H%M')
            print(date)
        except ValueError as err:
            print(fileTime,err)
    
    # 2010-09-21 11:00:00
    # ('201009211199', ValueError('unconverted data remains: 9',))
    

    Or, if you install the third-party module, dateutil, you could parse it like this:

    In [106]: import dateutil.parser as dparser
    
    In [107]: dparser.parse('201009211100')
    Out[107]: datetime.datetime(2010, 9, 21, 11, 0)
    

    Notice that dateutil tries to parse the string without you explicitly declaring the format. This has to be used carefully (with testing and control over admissible input strings) since otherwise there are ambiguous dates which dateutil may parse incorrectly.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

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.