so I created a book class and added its instances to a list that I'm trying to pickle but I'm getting a strange error. Help appreciated!
with open("book_data/books.pkl", "wb") as f:
pickle.dump(book_list, f)
Output:
PicklingError Traceback (most recent call last)
<ipython-input-24-45ad0cec62df> in <module>
1 with open("book_data/books.pkl", "wb") as f:
2
----> 3 pickle.dump(book_list, f)
PicklingError: Can't pickle <class '__main__.book'>: it's not the same object as __main__.book
Related
I am trying to read documents from a file path using a Jupyter Notebook as follows.
train_art_path = "/work/TEXT_SUMMARIZATION/train_dialogue.txt"
with open(train_art_path, 'r') as file:
a=file.readlines()
print(a)
print('\n')
I get the following issue:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
/tmp/ipykernel_79659/545421924.py in <module>
----> 1 with open(train_art_path, 'r') as file:
2 a=file.readlines()
3 print(a)
4 print('\n')
FileNotFoundError: [Errno 2] No such file or directory: 'train_art_path'
Alternatively, I tried to read documents as follows. It works perfectly.
with open('train_dialogue.txt', 'r') as file:
a=file.readlines()
print(a)
print('\n')
Can you please help me to solve the first one? Thanks in Advance.
You are using train_art_path as a variable, so should not have quotes ' after assignment. Change...
with open('train_art_path', 'r') as file:
to
with open(train_art_path, 'r') as file:
I am trying to learn how to open txt.files on Python 3.7.
I have a file (Days.txt) with the following content:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
On Jupyter, I used the following code:
f = open('Days.txt','r').read()
f
which gave me the following result:
'Monday\nTuesday\nWednesday\nThursday\nFriday\nSaturday\nSunday'
so far, so good.
Then, I tried to add a sentence to the Days.txt file by:
f.write('this is a test\n')
and that's where I get the below error message:
AttributeError Traceback (most recent call last)
in
----> 1 f.write('this is a test\n')
AttributeError: 'str' object has no attribute 'write'
Why isn't it working?
The code below results in another error message:
file = open('Days.txt','r')
s = file.read()
file.write('this is a test\n')
file.close()
---------------------------------------------------------------------------
UnsupportedOperation Traceback (most recent call last)
<ipython-input-138-0cc4cd12a8b3> in <module>
1 file = open('Days.txt','r')
2 s = file.read()
----> 3 file.write('this is a test\n')
4 file.close()
UnsupportedOperation: not writable
Because f is a string, not the file pointer, it's better to keep the file pointer in its own variable:
f = open('Days.txt', 'r')
s = f.read() # <-- check the content if you need it
print(s)
f.write('this is a test\n')
f.close() # <-- remember to close the file
Better yet to automatically close the file:
with open('Days.txt', 'r') as f:
s = f.read()
print(s)
f.write('this is a test\n')
Open the file in write mode to allow adding new contents to it.
To do so, just
f = open("Days.txt", "a")
f.write("This is a text\n")
f.close()
Remember to close the file handler after opening it.
I however suggest you to use the "with" construct, that automatically closes the file handler after the read/write operation
with open("Days.txt", "a") as f:
f.write("This is a text\n")
I'm learning Python and it's early days for me. The following small bit of code won't run, the error message is
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'logdata' is not defined
The file is called "logdata.py". The faulty(?) code is;
def logthis(addme):
f=open("log.txt", "a+")
f.write(addme)
f.close()
logthis('teststring')
If there is a better place for a basic question like this please let me know, I'm sure i'll have plenty more to come as i learn Python, thanks!
I think, you have some extra lines of code in beginning of file which uses undefined identifier (variable, function etc.) with name logdata. Something like this.
>>> def f():
... print(logdata)
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
NameError: name 'logdata' is not defined
>>>
If so, just define/initialize that. Finally, your code will work fine then as I have already tested it as follows.
>>> def logthis(addme):
... f = open("log.txt", "a+")
... f.write(addme)
... f.close()
...
>>> logthis('teststring')
>>>
>>> # Read the newly created file content
...
>>> f = open("log.txt", "r")
>>> f.read()
'teststring'
>>>
I read "How to think like a Computer Scientist. Learning with Python." book. So I usually have no difficulties to interpret examples from python2 to python3, but at chapter 11 Files & Exceptions I encountered this snippet
>>> import pickle
>>> f = open("test.pck", "w")
>>> pickle.dump(12.3, f)
>>> pickle.dump([1,2,3], f)
>>> f.close()
which when I evaluate it using Python 3.5.2 gives this error
Traceback (most recent call last): File "/(myDirs)/files.py", line 3, in <module>
pickle.dump(3.14, f)
TypeError: write() argument must be str, not bytes
I am not a good docs reader, so if you can help me to solve this riddle I would be grateful.
You need to open the file in binary mode.
In line 2:
f = open("test.pck", "wb")
I have uploaded a large csv into python using the following code:
ddgqa = pd.read_table("/Users/xxxx/Documents/globalqa-pageurls-Report.csv", chunksize= 10**6, iterator=True)
I am now extract a column into a dataframe using this code:
for chunk in ddgqa:
links_adobe_ddgqa = pd.DataFrame(ddga['Page URL'])
When I try to links_adobe_ddgqa
I get the following error:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-95-1ab6eaf23d5f> in <module>()
----> 1 links_adobe_ddgqa
NameError: name 'links_adobe_ddgqa' is not defined
What am I missing? Is there a better way to accomplish this?