Help

Re: How to programatically import CSV to AirTable, creating a new table in an existing base (CSV is a file on a cloud VM)?

4387 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Dev_Local
6 - Interface Innovator
6 - Interface Innovator

I am sure this has been asked before, I have searched high and low, and have found no solution to do the following, even with Zapier.

I have a .csv file that I want to import direct to Airtable.

The .csv was created programatically (in Node.js), and exists on a Google Cloud VM (Red Hat Linux).

There is nothing in the AirTable API that allows for direct insertion of a full CSV as a new tab in an existing base.

I want to name the tab in the existing base the same name as the CSV file, without the .csv extension.

If anyone has actually done this successfully, and smoothly, please respond. The only articles I can find on how to import a CSV to AirTable involve a human being and clicking in the browser.

23 Replies 23

I’m no API wizard (haven’t even used it yet), but just looking at the code, this styled apostrophe before your closing quote in the post_url string sticks out to me. Could that be part of the problem?

Screen Shot 2020-05-28 at 9.56.02 AM

Good eye! That would cause a 422 I think.

Definitely would be the issue, unfortunately that was just a typo on my part as I wrote the post :slightly_smiling_face:

Will keep troubleshooting, thanks for the responses all!

Hi @Ryan_Wigley - I think the answer lies with the comment made by @Bill.French. I think the python cvs parser treats everything as a string, hence, you are trying to put this:

'949.7318' into a number field and it won’t go. However, if you do this:

inventory = float(row[0])

and convert it to a float before sending to AT, it should work. You can convert floats and integers - I think the rest will be fine as strings. (I have an integer conversion in my script above).

JB

@Ryan_Wigley - side point - and this might just be the formatting on the post rather than your script, but the last 4 lines of your script are outdented, so will only run once after the looping has occurred - you need to indent them into the loop to run on each iteration. Probably not an issue as your console is showing multiple requests, but thought I’d mention it

@JonathanBowen @Bill.French

Thanks all, that did the trick. It was a matter of shaping the corresponding columns as floats / integers. I did not realize I could pass a partial POST request (i.e. not include all fields at once) so it was very helpful that you pointed it out Bill. Code works like a charm now!

paul_de_paula
4 - Data Explorer
4 - Data Explorer

I’m getting an error using the script above
Traceback (most recent call last):
File “update_airtable.py”, line 42, in
post_airtable_request = requests.post(post_url, headers = post_headers, json = data)
AttributeError: ‘str’ object has no attribute ‘post’

import requests, json
import csv
import os, sys

post_url = 'https://api.airtable.com/v0/xxxxxx/LT'
post_headers = {
    'Authorization' : 'Bearer keyxxxxxxxxxxx',
    'Content-Type': 'application/json'
}

branch = sys.argv[1]
filename = branch + '_stats.csv'
f = open(filename)
csv_f = csv.reader(f)
for row in csv_f:
    method = row[0] 
    name = row[1] 
    requests = row[2] 
    failures = row[3] 
    median_rt = row[4] 
    avg_rt = row[5]
    min_rt = row[6]
    max_rt = row[7]
    avg_content_size = row[8]
    request_ps = row[9]
 
    data = { 
        "fields": {
            "Name": name,
            "Requests": requests,
            "Failures": failures,
            "Median Response Time": median_rt, 
            "Average Response Time": avg_rt, 
            "Minimum Response Time": min_rt, 
            "Maximum Response Time": max_rt,
            "Average Content Size": avg_content_size,
            "Requests Per Second": request_ps
        }
    } 
    print(post_url)
    print(data)
    post_airtable_request = requests.post(post_url, headers = post_headers, json = data)
    print(post_airtable_request.fields)
    print(post_airtable_request.status_code)

I’m somewhat fluent in Python, but I suspect the issue is that data needs to be serialized (which I believe is json.dumps()). Since dumps() performs some pretty-printing stuff, I think you also have to change the separators to make it behave more like json.stringify().

Try this…

post_airtable_request = requests.post(post_url, headers = post_headers, json =
json.dumps(data, separators=(',', ':')))

I tried the snippets you shared and this one

post_airtable_request = requests.post(post_url, headers = post_headers, json = json.stringify(data))
I’m still having the same error

Moe
10 - Mercury
10 - Mercury

If you have a URL for a csv file that you’d like to import in Airtable continuously, we’ve built an extension that handles this.