On Wed, Jan 25, 2012 at 11:30 AM, ken brockman <krush1...@yahoo.com> wrote: > Okay, got ya. Mea Culpa. > Being misanthropic by nature I've not used or engaged in a interactive > forum such as this one before . > I do grasp that a list is more then just the information it contains. So, > does that mean i go with pickling? And can I save two lists in one pickled > file, or no? > > Thanks one and all, for the help. > Ken > ________________________________ > From: Joel Goldstick <joel.goldst...@gmail.com> > To: ken brockman <krush1...@yahoo.com> > Cc: tutor@python.org > Sent: Wednesday, January 25, 2012 11:04 AM > > Subject: Re: [Tutor] how to read and write to a file > > On Wed, Jan 25, 2012 at 10:50 AM, ken brockman <krush1...@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.goldst...@gmail.com> >> To: Alexander <rhettna...@gmail.com> >> Cc: ken brockman <krush1...@yahoo.com>; tutor@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 <rhettna...@gmail.com> wrote: >>> On Wed, Jan 25, 2012 at 8:32 AM, ken brockman <krush1...@yahoo.com> >>> wrote: >>>> >>>> >>>> ________________________________ >>>> From: Alexander <rhettna...@gmail.com> >>>> To: ken brockman <krush1...@yahoo.com> >>>> Cc: "tutor@python.org" <tutor@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 <krush1...@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@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@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 > > Go to the link I listed above. It has examples. I think you can use one file and keep writing pickles to it. But I've not used pickling, so if it was me I would play around with it and see what you find out. The link has a simple example and other links to more complex situations.
-- Joel Goldstick _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor