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 ?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 24, 2014 at 16:06
5
  • What shapefile/python module are you using? Commented Jun 24, 2014 at 16:09
  • Pyshp but what is a (a[8:10] etc.) ? Commented Jun 24, 2014 at 16:23
  • Hi The column time 4th is like "2014年05月30日T16:32:39+0000" and I´d like to format it as '30 / 16:32 UTC' to show this in the label. Commented Jun 24, 2014 at 16:37
  • I´m using pyshp module Commented Jun 24, 2014 at 16:54
  • look for solution below Commented Jun 25, 2014 at 17:59

2 Answers 2

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)

answered Jun 25, 2014 at 17:59
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.

answered Jun 27, 2014 at 13:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.