We help IT Professionals succeed at work.
Get Started

Reformatting lat/longs in a csv file

sara_bellum
sara_bellum asked
on
565 Views
Last Modified: 2012-05-05
I have a csv file with a list of stations and station parameters in each row, and I want to change the lat/long format from this:  70 43.311 N to this: N 70 43.311
I took a stab at it, but it would take me a very long time to figure out how this might work. Pls point me in the right direction, thanks!
import csv 
 
fin = open('weatherInfo.csv', 'rb')  # read binary
source = csv.reader(fin, quoting=csv.QUOTE_NONNUMERIC)    # quoting argument
fout = open('newWeather.csv', 'wb') # write binary
dest = csv.writer(fout, quoting=csv.QUOTE_NONNUMERIC)
index = 1 # leave the header out of this
lat = "N"
lon = "W"
for i, row in enumerate(source):
    if lat in row[4]:
        str = row[4:]
        str[:-1]     #Truncate the N from the end of each latitude, ex 70 43.311 N
        str.insert(0, lat) #Insert the N back into the beginning, ex N 70 43.311
        print row[4]  #Just to check the output
    if lon in row[5]:  
        # repeat the above steps for longitudes
        dest.writerow(row) # Write the results to the output file
fin.close()
fout.close()

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 2 Answers and 8 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE