1,105,290 Community Members

Help using xlwt format module

Member Avatar
jarograv
Light Poster
40 posts since Apr 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
woooee
Posting Maven
2,792 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
0
 

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
Member Avatar
woooee
Posting Maven
2,792 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
1
 

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(":")
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: