I have this code:

import csv
tally=0
i=0
n=1
numb=2
count = sum(1 for row in csv.reader( open('cities.csv') ) )
cityname = csv.reader(open("cities.csv", "rb"))
citynames = []
citynames.extend(cityname)
for data in citynames:
    citynames.append(data[0])
    tally+=1
    if tally>=count:
        break
tally=0

cities_x = csv.reader(open("cities.csv", "rb"))
cities_list_x = []
cities_list_x.extend(cities_x)
names = []
for data in cities_list_x:
    names.append(data[0]) 

timel=raw_input("When is your target landing time?<'0' to exit> ")
first_colonl=timel.find(":")
hoursl=timel[:first_colonl]
minutesl=timel[first_colonl+1:first_colonl+3]
secondsl=timel[first_colonl+4:first_colonl+6]
hours_secondsl=int(hoursl)*3600
minutes_secondsl=int(minutesl)*60
total_secondsl=hours_secondsl+minutes_secondsl+int(secondsl)    
    
w = csv.writer(open('timer.csv','w'))
spamWriter = csv.writer(open('timer.csv', 'wb'), delimiter=' ',
                        quotechar=' ', quoting=csv.QUOTE_MINIMAL)    
spamWriter.writerow(['city'] + [','] + ['travel time'] + [','] + ['send Time'] + [','+timel] ) 
    
while i in range(0,count):
    time=raw_input("How long does it take to get to your target from "+str(names[i])+"?<'0' to exit> ")
    first_colon=time.find(":")
    hours=time[:first_colon]
    minutes=time[first_colon+1:first_colon+3]
    seconds=time[first_colon+4:first_colon+6]
    hours_seconds=int(hours)*3600
    minutes_seconds=int(minutes)*60
    total_seconds=hours_seconds+minutes_seconds+int(seconds)
    send_time=total_secondsl-total_seconds
    send_hours=send_time/3600
    send_minutes=(send_time-(send_hours*3600))/60
    send_seconds=(send_time-((send_hours*3600)+(send_minutes*60)))
    print "The send time is", send_hours,":", send_minutes,":",send_seconds
    sendtime='=D1-'+chr(98)+str(numb)+'+48'
    spamWriter.writerow([names[i]] + [','] + [sendtime] + [','] + [time])
    i+=1
    numb+=1

I need to format the variable 'sendtime' into hh:mm:ss so that it does not show up as a number with decimals in my spreadsheet. If anybody can provide me with code or explain the xlwt module better than their help manuals do that would be helpful.

Thanks,


Jaro

Recommended Answers

All 2 Replies

Although we don't know the format of the file, this is probably a never-ending loop as you are iterating over, and appending to, the same list.

for data in citynames:
    citynames.append(data[0])

Run this snippit as it limits the list to a length of 100, instead of an infinite loop.

x = [ 1, 2, 3 ]
for ctr, el in enumerate(x):
    if len(x) < 100:  
        x.append(el)
        print ctr
print x

Using the datetime module is an easy way to subtract one time value from another IMHO.

import datetime

# subtract 10:02:59 from 12:01:02
difference = datetime.datetime(2011, 5, 11, 12, 1, 2) - \
             datetime.datetime(2011, 5, 11, 10, 2, 59)
print "the difference is %s" % (str(difference))

#
# also note that split(":") is a better way to split the input
# and you can also check for len == 3
time_input = "2:03:05"
print time_input, "-->", time_input.split(":")
commented: agree +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.