-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_csv.py
More file actions
73 lines (58 loc) · 2.28 KB
/
import_csv.py
File metadata and controls
73 lines (58 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from skyflow.errors import SkyflowError
from skyflow.service_account import generate_bearer_token, is_expired
from skyflow.vault import Client, InsertOptions, Configuration
import csv
vault_id = '-skyflow_vault_id-'
account_id = '-skyflow_account_id-'
vault_url = '-skyflow_vault_url-'
table_name = 'employees'
# cache token for reuse
bearerToken = ''
def token_provider():
global bearerToken
if is_expired(bearerToken):
bearerToken, _ = generate_bearer_token('credentials.json')
return bearerToken
try:
config = Configuration(
vault_id, vault_url, token_provider
)
client = Client(config)
options = InsertOptions(True)
# Open the input CSV file
with open('input.csv', mode='r') as infile:
csv_reader = csv.reader(infile)
# Read and store the header row
header = next(csv_reader, None)
# Open the output CSV file in write mode once
with open('output.csv', mode='w', newline='') as outfile:
csv_writer = csv.writer(outfile)
# Write the header to the output file
if header:
csv_writer.writerow(header)
# Loop through the records in the input file
for row in csv_reader:
employee_id = row[0] # Assuming 'id' is in the first column
name = row[1] # Assuming 'name' is in the second column
data = {
"records": [
{
"table": "employees",
"fields": {
"name": name,
"employee_id": employee_id
}
}
],
"tokenization": 'true'
}
# Insert data using the Skyflow client
response = client.insert(data, options=options)
print('Response:', response)
# Update the 'name' field in the row with the response
row[1] = response['records'][0]['fields']['name']
print('Tokenized Name:', row[1])
# Write the updated row to the output file
csv_writer.writerow(row)
except SkyflowError as e:
print('Error Occurred:', e)