Heisenberg
Heisenberg

Reputation: 8816

How to use an external, static data file in a script hosted on Google App Engine?

I have a Python script hosted on Google App Engine to access Twitter API and store the results.

I have a static csv file (on my local machine) that contains the name of all the twitter accounts that I want to collect data from.

How to make this file accessible to my script hosted on Google App Engine?

Upvotes: 0

Views: 107

Answers (2)

GAEfan
GAEfan

Reputation: 11370

If you don't want to deal with the inconvenience of uploading your code every time you make a change to your CSV file, you could maintain it as a Google Sheet, one value per cell. Publish it to web. Then, read it into your app like this:

from google.appengine.api import urlfetch

csv_url = 'https://docs.google.com/spreadsheets/d/<key>/export?format=csv'

csv_response = urlfetch.fetch(csv_url, allow_truncated=True) 

if csv_response.status_code == 200:
  row_list = csv_response.content.split('\n')
  for row in row_list:
    item_list = row.split(',')

    do stuff

Upvotes: 1

mgilson
mgilson

Reputation: 310227

The static CSV should be uploadable the same as any of your other python source files. Note, do not upload it as a static file -- Those generally serve from a different place and your code does not have access to them1.

To open/read the static CSV, you'll want to use a relative path. e.g. If your directory looks like this:

foo
 | - script.py
bar
 | - data.csv

Then to read data.csv from script.py, you'd do something like:

import os
data_csv_filename = os.path.join(
    os.path.dirname(__file__), 'bar', 'data.csv')
with open(data_csv_filename) as data_csv_file:
    ...

1You could alternatively set it as "application_readable" in app.yaml if you actually want to serve the csv file as well as have your application read the data...

Upvotes: 1

Related Questions