1

I´m doing a script to convert csv to shp, but I want to format one of the columns. It´s a string then I´d like to format like this

a[8:10]+' / '+a[11:16]+' UTC' being a the value in the column.

this is the code working

#Set up blank lists for data
nombre,codigo,y,x,time=[],[],[],[],[]

#read data from csv file and store in lists
with open('C:/archivecsvToRead.csv', 'r') as csvfile:
    r = csv.reader(csvfile, delimiter=',')
    for i,row in enumerate(r):
        if i > 0: #skip header
           nombre.append(row[0])
           codigo.append(row[1])
           y.append(float(row[2]))
           x.append(float(row[3]))
           time.append(row[4])


#Set up shapefile writer and create empty fields
w = shp.Writer(shp.POINT)
w.autoBalance = 1 #ensures gemoetry and attributes match
w.field('Longitud','F',10,8)
w.field('Latitud','F',10,8)
w.field('Date','D')
w.field('Date / Time','C',50)
w.field('ID','N')

#loop through the data and write the shapefile
for j,k in enumerate(x):
    w.point(k,y[j]) #write the geometry
    w.record(k,y[j],codigo[j], time[j], nombre[j]) #write the attributes

#Save shapefile
w.save(out_file)

How can I format the column time in order to show a[8:10]+' / '+a[11:16]+' UTC' being 'a' the value in the column ?

  • What shapefile/python module are you using? – artwork21 Jun 24 '14 at 16:09
  • Pyshp but what is a (a[8:10] etc.) ? – gene Jun 24 '14 at 16:23
  • Hi The column time 4th is like "2014-05-30T16:32:39+0000" and I´d like to format it as '30 / 16:32 UTC' to show this in the label. – kamome Jun 24 '14 at 16:37
  • I´m using pyshp module – kamome Jun 24 '14 at 16:54
  • look for solution below – gene Jun 25 '14 at 17:59
3

Hi The column time 4th is like "2014-05-30T16:32:39+0000" and I´d like to format it as '30 / 16:32 UTC' to show this in the label.

For that you can use Regular expressions: you want to extract '30T16:32' from the string (This isn't geospatial but ...)

The pattern (regular expression to be matched) is [0-9]+T[0-9]+:[0-9]+or \d+T\d+:\d+(search for the regular expression numbers T numbers : numbers)

import re
a =  "2014-05-30T16:32:39+0000"
result = re.search(r'\d+T\d+:\d+',a)
print result.group()
'30T16:32'
print result.group()[0:2]
 '30'
print result.group()[3:]
'16:32'
print result.group()[0:2] + ' / ' + result.group()[3:] + 'UTC'
30 / 16:32 UTC

or

result = re.findall(r'\d+T\d+:\d+',a)
print result
['30T16:32']
# split the list
result = result[0].split('T')
print result[0] + ' / ' + result[1] + 'UTC'
30 / 16:32 UTC

or ... (many others pattern matching)

1

Usually you could use the inbuilt Python datetime library strptime method to read in the date/time string you have, but unfortunately it has issues when reading time zones from strings. Instead you can use the python-dateutil library to parse the date string, and the datetime.strftime library to write it out.

For example:

from datetime import datetime
from dateutil import parser

s = "2014-05-30T16:32:39+0000" #This is an ISO 8601 datetime string
d = parser.parse(s) #recognizes the 8601 format without having to specify it, returns a datetime object
print datetime.strftime(d, "%d / %H:%M %Z") #prints out "30 / 16:32 UTC"

In the long run I think it is better to manipulate the datetime object as it gives a lot more control than using string manipulation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.