0

Summary of probelm: I have a file with all zip codes in the US, the file is in the format "ZIP,LATITUDE,LONGITUDE\n' I would like to save each line as a model instance of:

class ZipCode(models.Model):
    zip_code = models.CharField(max_length=7)
    latitude = models.DecimalField(decimal_places=6, max_digits =12)
    longitude = models.DecimalField(decimal_places=6, max_digits =12)

without having to enter each manually by hand as this would take forever.

My attempt at a solution: Writing a view to handle this one time task does not seem like the best way to do this, but it is all I could think of, so I tried the following:

def update_zipcodes(request):
    zip_code_file = open('zip_codes.txt')

    #lists to hold zip code, longitude and latitude values:
    zip_codes = []
    latitude = []
    longitude = []

    for line in zip_code_file.readlines():
        #example line "00601,18.180555, -66.749961"
        zipcode, lat, lng = line.strip().split(',')
        zip_codes.append(zipcode)
        latitude.append(lat)
        longitude.append(lng)

    #convert lat and long to floats
    latitude = [float(i) for i in latitude]
    longitude = [float(i) for i in longitude]

    counter = 0
    for item in zip_codes:
        zip_code_object = ZipCode(zip_code=zip_codes[counter],latitude=latitude[counter],longitude=longitude[counter])
        zip_code_object.save()
        counter= counter+1

    return HttpResponse('working')

and it returned cannot find file 'zip_codes.txt' even though I put this file in the same folder as views. Clearly I don't know how to accomplish the goal at hand. Is there anyway to go about connecting to my database and uploading the values as specified? (original values can be seen at https://gist.githubusercontent.com/erichurst/7882666/raw/5bdc46db47d9515269ab12ed6fb2850377fd869e/US%2520Zip%2520Codes%2520from%25202013%2520Government%2520Data , I copied and pasted this into zip_codes.txt)

justin
  • 553
  • 1
  • 7
  • 18

1 Answers1

0

I'm not sure of your folder structure, but my educated guess would be the following. You are trying to open zip_codes.txt, which is a relative path, not an absolute path. An absolute path starts at the root directory. In a Linux system, this might look like /path/to/file/zip_codes.txt, whereas in a Windows system, this might look like C:\path\to\file\zip_codes.txt. Regardless, in Python you can use either. However, you must be careful when using relative paths. If you are searching for a file based on a relative path, Python will only look in the current working directory. Therefore, you need to specify a relative path from the current working directory to where the file is stored. As an example, if you had the following data structure:

project
  /folder
    view.py
    zip_codes.txt

If you ran python project/view.py (the current working directory is /project), where view.py is your file above, with open('zip_codes.txt'), Python would not be able to find the file because it is not in the current working directory. You could write the full path, if you know it, for instance open('folder/zip_codes.txt'). You could also change your working directory to the folder directory, and run python view.py directly from there. In this case, the program would be able to find the file because it is in the same working directory.

Another fix would be that if you still want to run the program from the same working directory as before, you could use the os module as shown in this post:

import os

filename = os.path.join(os.path.dirname(__file__), 'zip_codes.txt')