Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Dec 03, 2019 08:13 AM
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.
May 28, 2020 10:03 AM
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?
May 28, 2020 10:57 AM
Good eye! That would cause a 422 I think.
May 28, 2020 11:02 AM
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!
May 28, 2020 01:41 PM
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
May 28, 2020 01:43 PM
@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
May 28, 2020 02:49 PM
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!
Jun 17, 2020 10:01 AM
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)
Jun 17, 2020 01:59 PM
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=(',', ':')))
Jun 17, 2020 09:27 PM
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
Jun 18, 2020 03:46 AM
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.