Robbie Barrat
Robbie Barrat

Reputation: 520

Opening file as two separate parts in python

So I have a text file that is formatted as follows:

a, e, s, o, ea, ng, fr
ccopy_reg
_reconstructor
p0
(cpybrain.structure.networks.recurrent
RecurrentNetwork
blahblahblah more stuff down here for a few hundred lines

The first line is a list of values independent from the other information, but everything beyond the first line is a dump of a neural network, which was created with pickle but is loaded back into the network like:

fileObject = open('filename', 'r+')
net = pickle.load(fileObject)

If I try simply running this to open the file, I get an error because the list at the beginning messes it up. Is there any sort of function or method I could implement so that I don't have to have a temporary file, but I could get the string of the first line while also loading the rest with pickle?

EDIT: I would like to store the first line as a string variable

Upvotes: 1

Views: 57

Answers (1)

snow
snow

Reputation: 438

Use fileObject.readline() before the pickle.load(fileObject):

fileObject = open('filename', 'r+')
firstline = fileObject.readline()
net = pickle.load(fileObject)

Upvotes: 2

Related Questions