Comments (8)
@CatParky you should be able to accomplish this with a custom function that writes the Knack
data to csv.
Your function would look something like this:
def to_csv(data, fieldnames, filename, delimiter=","):
with open(filename, 'w', newline='\n') as fout:
writer = csv.DictWriter(fout, fieldnames=fieldnames, delimiter=delimiter)
writer.writeheader()
for row in data:
writer.writerow(row)
return None
With your function defined, you can do this to get your CSV:
# define your fieldnames
my_fieldnames = ['fieldname_1', 'fieldname_2', 'fieldname_3']
# write your data to `my_data.csv`
# see readme for more about the `kn.data` object
to_csv(kn.data, my_fieldnames, "my_data.csv")
from knackpy.
I'm sorry to bother you, but I just can't get this working.
Using tutorials I have saved the custom function as knackpy_to_csv.py in a folder and ensured that the text is exactly as stated above with no whitespace.
I have then used the attached file (security details removed) and when I uncomment the print(row) I can see that it is recieving all the data however I get the error :
Traceback (most recent call last):
File "Z:\System Administrator\System Backups\KNACK\Scripts\KNACK - Clients.py", line 29, in
to_csv(kn.data, my_fieldnames, "Clients.csv")
NameError: name 'to_csv' is not defined
As I am a real beginner at python I created and called a Hello World function from the same folder as knackpy_to_csv.py and it called just as expected.
Any advice would be very appriciated
Thank you so much for building this whole function
from knackpy.
@CatParky i think it would make your life easier to put all of you python code in one file.
#### import modules ####
from knackpy import Knack
#### define csv function ####
def to_csv(data, fieldnames, filename, delimiter=","):
with open(filename, 'w', newline='\n') as fout:
writer = csv.DictWriter(fout, fieldnames=fieldnames, delimiter=delimiter)
writer.writeheader()
for row in data:
writer.writerow(row)
return None
#### get knack data ####
kn = Knack(
obj='object_1',
app_id='abc123',
api_key='topsecretapikey'
)
#### define your fieldnames ####
my_fieldnames = ['fieldname_1', 'fieldname_2', 'fieldname_3']
#### # write your data to csv ####
to_csv(kn.data, my_fieldnames, "my_data.csv")
I'm not going to download your zip file. Please paste your code directly into a comment.
from knackpy.
Thank you for your reply, unfortunately I'm still getting the same error
Traceback (most recent call last):
File "Z:/System Administrator/System Backups/KNACK/Scripts/KNACK - Clients2.py", line 27, in
to_csv(kn.data, my_fieldnames, 'my_data.csv')
File "Z:/System Administrator/System Backups/KNACK/Scripts/KNACK - Clients2.py", line 7, in to_csv
writer = csv.DictWriter(fout, fieldnames=fieldnames, delimiter=delimiter)
NameError: name 'csv' is not defined
I'm running Windows 10 using Python 3.7.1 and this is the code I copied from above
#### import modules ####
from knackpy import Knack
#### define csv function ####
def to_csv(data, fieldnames, filename, delimiter=","):
with open(filename, 'w', newline='\n') as fout:
writer = csv.DictWriter(fout, fieldnames=fieldnames, delimiter=delimiter)
writer.writeheader()
for row in data:
writer.writerow(row)
return None
#### get knack data ####
kn = Knack(
obj='object_2',
app_id='MyID',
api_key='MyKey'
)
#### define your fieldnames ####
my_fieldnames = ['Client_ID', 'Client Name', 'Client Three Letter Ref']
#### # write your data to csv ####
to_csv(kn.data, my_fieldnames, "my_data.csv")```
I know that the data is being returned as when I run the print command I can see all the data scroll. I can also see that the my_data.csv file is being created, however it's blank.
Thank you again
from knackpy.
@CatParky you need to add import csv
at the beginning of your code.
from knackpy.
I tried that and then get the error
Traceback (most recent call last):
File "Z:/System Administrator/System Backups/KNACK/Scripts/KNACK - Clients2.py", line 28, in
to_csv(kn.data, my_fieldnames, "my_data.csv")
File "Z:/System Administrator/System Backups/KNACKScripts/KNACK - Clients2.py", line 13, in to_csv
writer.writerow(row)
File "C:\Program Files\Python37\lib\csv.py", line 155, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "C:\Program Files\Python37\lib\csv.py", line 151, in _dict_to_list
+ ", ".join([repr(x) for x in wrong_fields]))
ValueError: dict contains fields not in fieldnames: 'Client Lead', 'id', 'Third Party', 'Client Type', 'Status'
The full list of field names from the Client table are
Client_ID, Client Name, Client Three Letter Ref, Client Type, Third Party, Status, Client Lead
When I change the fieldnames part of your code to include all fields
my_fieldnames = ['Client_ID', 'Client Name', 'Client Three Letter Ref', 'Client Type', 'Third Party', 'Status', 'Client Lead']
I get the error
Traceback (most recent call last):
File "Z:/System Administrator/System Backups/KNACK/Scripts/KNACK - Clients2.py", line 28, in
to_csv(kn.data, my_fieldnames, "my_data.csv")
File "Z:/System Administrator/System Backups/KNACK/Scripts/KNACK - Clients2.py", line 13, in to_csv
writer.writerow(row)
File "C:\Program Files\Python37\lib\csv.py", line 155, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "C:\Program Files\Python37\lib\csv.py", line 151, in _dict_to_list
+ ", ".join([repr(x) for x in wrong_fields]))
ValueError: dict contains fields not in fieldnames: 'id'
from knackpy.
Hi,
I'm sorry to bother you again. But do you think there is a resolution to this ? I can see there is a line in the original code : self.fieldnames.sort() is there any way I can comment this out ?
from knackpy.
@johnclary I was wondering if there is a solution to the above.
from knackpy.
Related Issues (20)
- Size constraints on knackpy.get() requests? HOT 8
- Downloads Function Documentation HOT 1
- Sanitize file paths when downloading files HOT 1
- Update connected field value HOT 1
- Unexpected behavior when using `Record.format`
- Ability to override default formatters
- Knack formatting for equation type fields is not preserved HOT 4
- Unable to run app.get() for some objects in a knack form due to datetime issue HOT 3
- Feature Request: add a formatter for user_roles field type
- Source field names from views
- Handle 5xx errors like timeouts
- Issue with get function when Knack fields updated as connection fields HOT 3
- Unable to make filters work HOT 2
- Support changing MAX_ROWS_PER_PAGE HOT 3
- api._continue has a infinite loop bug HOT 7
- Make App._download_files() a static method
- How to fill in knack "connection field" using knackpy
- How to fill in connection field (one to many) from knackpy HOT 2
- TypeError: string indices must be integers HOT 2
- MaxRetries Error while fetching 3.6 lakh records from Knack HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from knackpy.