0

I am trying to write my dictionary to a csv and i am getting errors, was wondering if someone could tell me what i am doing wrong

this is my function

n = open(file.csv,"w")
for item in my_dict.items():
		f.write(item)
f.close()

my my_dict is for example {john }

when i use the function i get the error: expected a character buffer object but if i replace the write with print it works.

thanks in advance

3
Contributors
9
Replies
10
Views
5 Years
Discussion Span
Last Post by woooee
0

I do not understand line 3, I think file class has not csv attribute. n is also never used.

0

im sorry i wrote it wrong

n = open("file.csv","w")
for item in my_dict.items():
		f.write(item)
n.close()

i get the error "error: expected a character buffer object"

but for example if i put it like this

n = open("file.csv","w")
for item in my_dict.items():
		print item
n.close()

it displays everything correctly on the screen, cant figure out what i am doing wrong

Edited by peste19: n/a

0

f is never declared and so is not a character buffer.

please see my updated post above yours

0

Please see line 3 in the first/top program in that post. If you had included the entire error message in the first place it would have shown that that was the error line and saved everyone some time.

Edited by woooee: n/a

0

i tried to edit the first post but it didnt allow me so i had to create another post and i actually included the error message in the first post.

Anyways can someone help with my problem?

Thanks in advance

n = open("file.csv","w")
for item in my_dict.items():
		n.write(item)
n.close()

Edited by peste19: n/a

0

A complete error message is like the following and greatly increases your chances of receiving some help.

my_dict = {1:"1"}
n = open('file.csv',"w")
for item in my_dict.items():
		f.write(item)
f.close()

"""
Traceback (most recent call last):
  File "./test_1.py", line 94, in <module>
    f.write(item)
NameError: name 'f' is not defined
"""
0

ok this is what i get

my_dict = {1:"1"}
>>> n = open('file.csv',"w")
>>> for item in my_dict.items():
	n.write(item)

	

Traceback (most recent call last):
  File "<pyshell#12>", line 2, in <module>
    n.write(item)
TypeError: expected a character buffer object

Edited by peste19: n/a

0

I've overlooked the obvious. It should be

for key, item in my_dict.items():

Hopefully it works now.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.