omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Error on writing to csv file

    Pythonista
    4
    18
    5903
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cvp
      cvp @jokiehints last edited by cvp

      @jokiehints I think your script is not in Pythonista but open as external file, for instance in iCloud Drive, and when you run it, it tries to create your data.csv file, what is not allowed outside of Pythonista

      Copy your script in Pythonista it-self, and you will see it running successfully

      1 Reply Last reply Reply Quote 0
      • jokiehints
        jokiehints last edited by

        @cvp Thank you! this is indeed the solution! Since I have pythonista I have worked within de directory phytonista. But.... not with this one! That's the reason why I couln't understand why the script didn't run. Now I do. Stupid me.

        cvp 1 Reply Last reply Reply Quote 0
        • cvp
          cvp @jokiehints last edited by

          @jokiehints If you develop in Python, you can't be stupid, only distracted like all scholars 😂

          1 Reply Last reply Reply Quote 0
          • cvp
            cvp last edited by

            When you edit a script, you can always ask where it is, even if Open as external file

            1 Reply Last reply Reply Quote 0
            • jokiehints
              jokiehints last edited by

              @cvp Thanks for your reply. Can I ask you another question? I hope I'm not asking too much. If this is the case, please forgive me asking.

              It regards the code below:

              import csv
              
              kolom = 'geb_dag'
              value = 19
              
              with open('data.csv', 'a', newline='') as csvfile:
                  fieldnames = ['column', 'value']
                  writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
              
                  writer.writeheader()
                  writer.writerow({'column': kolom, 'value': value})```
              
              This code gives an addition tot my data.csv file like: 
              
              column,value
              geb_dag,19
              
              I want to use this code several times to add more values to 'data.csv' like
              
              column,value
              geb_dag,19
              geb_mnd, 10
              geb_jr, 1981
              vnaam, 2
              anaam, 5
              
              But every time I give the variables 'kolom' (which stands for columnname) and 'value' (which stand for values I want to add) other strings or integers I get: 
              
              column,value
              geb_dag,19
              column,value
              geb_mnd, 10
              column,value
              geb_jr, 1981
              column,value
              vnaam, 2
              column,value
              anaam, 5
              
              At first: I want to get rid of the "column, value" row every time I use the code. 
              Second: Even beter I want the CSV go to a horizontal presentation like:
              
              geb_dag, geb_mnd, geb_jr, vnaam, a_naam,
              19, 10, 1981, 2, 5
              
              But what ever I do, it fails. Do you know an easy way to make this happen working with csv?
              
              I have a code that is working. But this is written using pandas and that  doesn't work in pythonista. For that reason I want to use CSV, so I can use my python code on my mobile.
              
              Thank you so much
              
              With kind regards.
              mikael cvp 2 Replies Last reply Reply Quote 0
              • mikael
                mikael @jokiehints last edited by mikael

                @jokiehints, first check if the file exists. Only writeheader if it did not.

                1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @jokiehints last edited by

                  @jokiehints Oups, I was able to help you for technical problem but, sincerely, I don't know anything about csv 😢
                  I hope somebody else will advice you. I'll follow your topic and if nobody doesn't (but I'ld be very surprised if it happens), I'll try to help you.

                  1 Reply Last reply Reply Quote 0
                  • jokiehints
                    jokiehints last edited by

                    @cvp OK thanks, I understand. Ik hope somewone else can give me the tips I need.

                    cvp 1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @jokiehints last edited by

                      @jokiehints Really not sure that I correctly understood, if not, sorry and forget this post

                      import csv
                      import os
                      
                      values = {}
                      def set(fieldname,value):
                      	values[fieldname] = value
                      		
                      set('geb_dag',10)
                      set('geb_maand',19)
                      
                      file = 'data.csv'
                      header = not os.path.exists(file)
                      
                      with open(file, 'a', newline='') as csvfile:
                          fieldnames = ['geb_dag','geb_maand']
                          writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
                      
                          if header:
                            writer.writeheader()
                          writer.writerow(values)
                      
                      1 Reply Last reply Reply Quote 0
                      • jokiehints
                        jokiehints last edited by

                        @cvp Thank you so much for your effort. Unfortunately this code isn't working for the format I want to get for my CSV file (as I described above). But please don't worry. I will continue with my search for a solution for this. Thanks again.

                        1 Reply Last reply Reply Quote 0
                        • JonB
                          JonB last edited by

                          What is the problem with cvp's solution? Does it still write the extra header row?

                          Your problem was that you were calling

                          writer.writeheader()
                          

                          When you were appending. Presumably that is now solved... But is there another issue?

                          cvp 1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @JonB last edited by cvp

                            @JonB In his first post after my answer about external files, he describes two problems a first and a second...
                            And I'm not sure to understand the second

                            Second: Even beter I want the CSV go to a horizontal presentation like:
                            
                            1 Reply Last reply Reply Quote 0
                            • JonB
                              JonB last edited by

                              I'll point out that you should delete the data.csv after implementing the changes, otherwise the old errors will still be there

                              cvp 1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp @JonB last edited by

                                @JonB I did it before each test, hoping he did the same

                                1 Reply Last reply Reply Quote 0
                                • mikael
                                  mikael last edited by

                                  I think he actually wanted a ”transposed” layout.

                                  @jokiehints said:

                                  geb_dag, geb_mnd, geb_jr, vnaam, a_naam,
                                  19, 10, 1981, 2, 5

                                  Which should be an easy change to @cvp’s code.

                                  1 Reply Last reply Reply Quote 0
                                  • JonB
                                    JonB last edited by

                                    ahh, in that case, you cannot use append for the file mode, but instead must read the whole file in first, append the date, then write it out again. ..

                                    cvp 1 Reply Last reply Reply Quote 0
                                    • cvp
                                      cvp @JonB last edited by

                                      @JonB yes but only if you change fields names, not if you add new records to existing variables.

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post
                                      Powered by NodeBB Forums | Contributors