GithubHelp home page GithubHelp logo

Comments (8)

johnclary avatar johnclary commented on July 17, 2024

@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.

CatParky avatar CatParky commented on July 17, 2024

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

Python Files.zip

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.

johnclary avatar johnclary commented on July 17, 2024

@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.

CatParky avatar CatParky commented on July 17, 2024

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.

hmnd avatar hmnd commented on July 17, 2024

@CatParky you need to add import csv at the beginning of your code.

from knackpy.

CatParky avatar CatParky commented on July 17, 2024

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.

CatParky avatar CatParky commented on July 17, 2024

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.

CatParky avatar CatParky commented on July 17, 2024

@johnclary I was wondering if there is a solution to the above.

from knackpy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.