[Tutor] how to read and write to a file
Joel Goldstick
joel.goldstick at gmail.com
Wed Jan 25 17:04:43 CET 2012
On Wed, Jan 25, 2012 at 10:50 AM, ken brockman <krush1954 at yahoo.com> wrote:
> Thank you Joel and Alexander for your time and input.
> I had just wanted to be able to store a list and read and write to it. But
> it seems reading and writing is no go with lists, only strings, or no? Is
> there a simply way I can tweak the read write feature to do lists? I'm sorry
> if I seem a tad slow on the uptick, I'm just starting to get
> into programming and being up all night is not helping my comprehension
> skills none. I've not as yet tried either of your suggestions, but from
> what I can gather pickling is not the right tool for the job at hand. I'm
> not looking to have it read by another program nor send it over a network.
>
> Also I have two lists and two files. Is it possible to store both lists in
> one file? I wouldn't imagine you could, but hey, doesn't hurt to ask.
> Thanks again.
>
> Ken
>
> ________________________________
> From: Joel Goldstick <joel.goldstick at gmail.com>
> To: Alexander <rhettnaxel at gmail.com>
> Cc: ken brockman <krush1954 at yahoo.com>; tutor at python.org
> Sent: Wednesday, January 25, 2012 9:54 AM
>
> Subject: Re: [Tutor] how to read and write to a file
>
> On Wed, Jan 25, 2012 at 9:14 AM, Alexander <rhettnaxel at gmail.com> wrote:
>> On Wed, Jan 25, 2012 at 8:32 AM, ken brockman <krush1954 at yahoo.com> wrote:
>>>
>>>
>>> ________________________________
>>> From: Alexander <rhettnaxel at gmail.com>
>>> To: ken brockman <krush1954 at yahoo.com>
>>> Cc: "tutor at python.org" <tutor at python.org>
>>> Sent: Wednesday, January 25, 2012 7:38 AM
>>> Subject: Re: [Tutor] how to read and write to a file
>>>
>>>
>>>
>>> On Wed, Jan 25, 2012 at 7:19 AM, ken brockman <krush1954 at yahoo.com>
>>> wrote:
>>>
>>> I would like to write to and read from a file from python. I wanted to
>>> use the file to save input to the program in a list. I have been looking
>>> around and there seems to be several ways to go about it. I tried pickling,
>>> but am having an issue with it. What would be the correct way to accomplish
>>> this? I have tried several ways, but to no avail. I get no error msg. but
>>> the list isn't retaining the info. Is pickling even the best way to do it.
>>>
>>> file1 = open("ArtyKlikes.p", "ab") # likesList
>>> file2 = open("ArtyDislikes.p", "ab") # dislikes
>>>
>>> pickle.dump(likesList, file1)
>>> pickle.dump(dislikeList, file2)
>>>
>>> file1.close()
>>> file2.close()
>>>
>>> Any help would be greatly appreciated.
>>>
>>> _______________________________________________
>>> Tutor maillist - Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>>
>>> Hi Ken. If you just want to read and write from a text file then you
>>> don't need to pickle.
>>> For example,
>>> (the file info.txt exists)
>>>
>>> >>>fh = open ( 'info.txt', 'w' )
>>> >>>fh.write ( 'peter piper picked a pack of pickled peppers.' )
>>> >>>fh.close()
>>> >>>fr = open ( 'info.txt', 'r')
>>> >>>fr.readline()
>>> 'peter piper picked a pack of pickled peppers.'
>>> >>>fr.close()
>>>
>>> or whatever.
>>> But do you have a need to use pickling?
>>> --
>>> Alexander
>>> 7D9C597B
>>>
>>> --------------------------------------------------------------------------
>>>
>>> Hey Alexander,
>>> I had to try it before I went to sleep.
>>> No good. I got an error msg. TypeError: must be str, not list.
>>> So I guess that may be why i had went with pickling. I needed something
>>> that would work with a list. Unless there is some other way?
>>> Thanks again for taking the time to help out.
>>>
>>> Ken
>>>
>>>
>> Ken, pickling is used when one wants to send information through a
>> network or communicate with a database. Somebody else here knows more
>> about pickling than I do. As for your list problem... I'm not exactly
>> certain what you're trying to do. But I'm going with the idea that you
>> have two files of information, one contains strings you like, and the
>> other contains strings you dislike. And you want to read and write
>> this information using Python.
>>
>>>>> like = [ 'orange', 'blue', 'red' ] #things I like
>>>>> dislike = [ 'apples', 'bronze', 'bananas' ] #things I dislike
>>>>> fh = open ( 'likes.txt', 'w' ) #let's open a file stream to write
>>
>> # fh is my shorthand for "file handle"
>> #writing a list to a file stream:
>>
>>>>> for index in range( len( like )):
>> fh.write( like[ index ] )
>> fh.write ( '\n' ) #here i add a new line, maybe somebody else
>> #knows a better way to avoid this?
>>
>>>>> fh.close()
>>
>> #now let's read that information into a list
>>>>> fr = open ( 'info.txt' ) #I'm using python 3.2
>>>>> mylistoflikes = fr.readlines()
>>>>> mylistoflikes
>> [ 'orange\n' , 'blue\n' , 'red\n' ]
>>>>> fr.close()
>>
>> --
>> Alexander
>> 7D9C597B
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>
> First, pickle is used to store data objects. If you just want to
> store text, you just write it and read it back from your file. But
> there are situations where you want to save an object. Maybe you want
> to have it available to another program.
>
> So, you nearly got through your example. You pickled your lists, and
> stored them, but you didn't retrieve them.
>
> Here is a link to review: http://wiki.python.org/moin/UsingPickle
>
> Here is my version of your program. It worked for me. copy and paste
> it in a file, and try it
> -----------------------------------------------
> import pickle
>
>
> ArtyKlikes = (1,2,3)
> ArtyKDislikes = (4,5,6)
>
> file1 = open("ArtyKlikes.p", "ab") # likesList
> file2 = open("ArtyDislikes.p", "ab") # dislikes
>
> pickle.dump(ArtyKlikes, file1)
> pickle.dump(ArtyKDislikes, file2)
>
> file1.close()
> file2.close()
>
> ArtyLikes = pickle.load(open("ArtyKlikes.p", 'rb'))
> ArtyDisLikes = pickle.load(open("ArtyDislikes.p", 'rb'))
>
> print ArtyLikes
> print ArtyDisLikes
>
> --
> Joel Goldstick
>
>
Ken,
First of all, always remember to reply to all on the list. That keeps
everyone in the loop.
Second, don't 'top post'. Write your comments at the end of the
thread so that people can follow the conversation. Sometimes its
useful to intersperse comments in someone's previous email.
Writing text to a file and reading it is useful, and pretty easy to
understand. But when you say you want to write a list you have to
start to understand what a list really is (or a dict or some other
object). A list is not only the values in the list, it is also the
code that lets a list do what lists do. Things like:
for something in mylist:
# do something with 'something'
or:
print mylist[2]
and so on.
So, pickle is doing a whole lot more than writing '(1,2,3)' to your
file. It knows it is a list and it stores all it needs to about the
list, including your specific list items.
--
Joel Goldstick
More information about the Tutor
mailing list