Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7682187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:30:36+00:00 2026-05-31T18:30:36+00:00

I’m using Python, and I have a file which has city names and information

  • 0

I’m using Python, and I have a file which has city names and information such as names, coordinates of the city and population of the city:

Youngstown, OH[4110,8065]115436
Yankton, SD[4288,9739]12011
966
Yakima, WA[4660,12051]49826
1513 2410
Worcester, MA[4227,7180]161799
2964 1520 604
Wisconsin Dells, WI[4363,8977]2521
1149 1817 481 595

How can I create a function to take the city name and return a list containing the latitude and longitude of the given city?

fin = open ("miles.dat","r")
def getCoordinates 
cities = []
for line in fin:
    cities.append(line.rstrip())
    for word in line:
        print line.split()

That’s what I tried now; how could I get the coordinates of the city by calling the names of the city and how can I return the word of each line but not letters?

Any help will be much appreciated, thanks all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:30:37+00:00Added an answer on May 31, 2026 at 6:30 pm

    I am feeling generous since you responded to my comment and made an effort to provide more info….

    Your code example isn’t even runnable right now, but from a purely pseudocode standpoint, you have at least the basic concept of the first part right. Normally I would want to parse out the information using a regex, but I think giving you an answer with a regex is beyond what you already know and won’t really help you learn anything at this stage. So I will try and keep this example within the realm of the tools with which you seem to already be familiar.

    def getCoordinates(filename):
        ''' 
        Pass in a filename.
        Return a parsed dictionary in the form of:
    
        {
            city:  [lat, lon]
        } 
        '''
    
        fin = open(filename,"r")
        cities = {}
    
        for line in fin:
    
            # this is going to split on the comma, and
            # only once, so you get the city, and the rest
            # of the line
            city, extra =  line.split(',', 1)
    
            # we could do a regex, but again, I dont think
            # you know what a regex is and you seem to already
            # understand split. so lets just stick with that
    
            # this splits on the '[' and we take the right side
            part = extra.split('[')[1]
    
            # now take the remaining string and split off the left
            # of the ']'
            part = part.split(']')[0]
    
            # we end up with something like: '4660, 12051'
            # so split that string on the comma into a list
            latLon = part.split(',')
    
            # associate the city, with the latlon in the dictionary
            cities[city] = latLong
    
        return cities
    

    Even though I have provided a full code solution for you, I am hoping that it will be more of a learning experience with the added comments. Eventually you should learn to do this using the re module and a regex pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.