Currently I am making code to be used to make my playing of an online conquest video game easier. The first program the user would run is the initialize program. This is how the program looks:

#initialize
import csv
x=1
y=1
n=0
citiesx=[]
citiesy=[]
city_names=[]
total=input("How many cities do you have? ")
while x in range(0,total+1):
   city_name=raw_input("What is the name of your city?<'0' exits> ")
   if city_name==0:
      break
   else:
      xcoord=input("What is the x coordinate?<'0' exits> ")
   if xcoord==0:
      break
   else:
      ycoord=input("What is the y coordinate?<'0' exits> ")
   if xcoord==0:
      break
   else:
      city_names.append(city_name)
      citiesx.append(xcoord)
      citiesy.append(ycoord)
      x+=1
w = csv.writer(open('cities.csv','w'))

spamWriter = csv.writer(open('cities.csv', 'wb'), delimiter=' ',
                        quotechar=' ', quoting=csv.QUOTE_MINIMAL)
while y in range(0,total+1):
   spamWriter.writerow([city_names[n]] + [','] + [citiesx[n]] + [','] + [citiesy[n]])
   n+=1
   y+=1
   
raw_input("press enter to exit")

The initialize program gets a list of coordinates and city names that are owned by the user in this video game. It organizes it in a .csv file that the user can access. The layout of the spreadsheet is like this(with commas being a new cell to the right):

City1,400,500
City2,450,550
City3,500,600

Next I have a program, called conquest timer, that requests the users target city (The town that they wish to attack with all their attacks landing at the same time). Then it asks for the travel time that it takes to get from each of their towns to their target town. This is what that program looks like:

#conquest timer
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-1):
    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
    spamWriter.writerow([names[i]+','+time+',=D1-'+chr(98)+str(numb)])
    i+=1
    numb+=1

The output of this program is saved as a .csv file that looks like this:

city travel time send Time 21:00:00
city1 20:00:00 #value
city2 19:50:00 #value
city3 18:55:34 #value

But I want it to format the cells (B2:B4-C2:C4 and D1) so that it looks like this:

city travel time send Time 21:00:00
city1 20:00:00 1:00:00 AM
city2 19:50:00 1:10:00 AM
city3 18:55:34 2:04:26 AM

I don't know if it is possible to somehow format it in python so that it is automaticly formated.

Thanks,


Jaro

Use datetime from datetime for time arithmetic and formating and shelve, json or cPickle for savĂ­ng Python data.

You can use normal print formating operations with % operator or .format method to write to file formated text without csv module. To keep space in data easiest is to use ';' as separator which rarely occurs in data or quoting of information.

I have not looked at this at all, beyond a quick 20 second scan of what they claim to offer, but it might be worth exploring: http://www.python-excel.org/

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.