For loops

This is the place for queries that don't fit in any of the other categories.

For loops

Postby mirocal » Tue Aug 05, 2014 12:25 pm

I am trying to write a 'for' loop to get my data to read through all the years and just select a certain few months which I need for my analysis. At the minute it looks like this
Code: Select all
first_year = 1979
last_year = 2013

for i in range(first_year, last_year+1)
     date_range[('str(i)-08-01_0000', 'str(i)-10-31_1800')]


So generally I want the time period of 01/08 to the 31/10 every year from 1979-2013, is there a better way of doing this???
mirocal
 
Posts: 5
Joined: Thu May 15, 2014 12:47 pm

Re: For loops

Postby metulburr » Tue Aug 05, 2014 12:52 pm

Code: Select all
for i in range(first_year, last_year+1)
     date_range[('str(i)-08-01_0000', 'str(i)-10-31_1800')]

Your str() is within commas, thus it is just a string. You want the result of a built-in function str() that returns a string. You also do not even need to change the data directly to a string as you can use the str.format() method. What is date_range suppose to be?

So generally I want the time period of 01/08 to the 31/10 every year from 1979-2013, is there a better way of doing this???

What do you mean by time period? Do you want the number of days, months, a list of dates, etc.?
http://lmgtfy.com/?q=python+get+months+ ... +two+dates

I would use the datetime module as opposed to for looping through
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 9 guests