Forum Archive

Error on writing to csv file

jokiehints

I get a error call running this script:

import csv

with open('data.csv', 'w', newline='') as csvfile:
    fieldnames = ['first_name', 'last_name']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})

There is a permission error [Errno 1] Operation not permitted: 'data.csv'.

Can someone please explain to me what I'm doing wrong?
Thank you so much for your assistence!

Kind regards

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

jokiehints

@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

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

cvp

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

jokiehints

@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

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

cvp

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

jokiehints

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

cvp

@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)
jokiehints

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

JonB

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

@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:
JonB

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

cvp

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

mikael

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.

JonB

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

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