Re: [Tutor] lists and strings
Hi Mike, Converting an (almost)arbitrary object into a string is what the Pickle module does. CPickle is faster. Take a look into into it in the docs. Here's an example: import cPickle lala = [1, 2, 3, 'four', 'V'] lala [1, 2, 3, 'four', 'V'] fileo = open('lala.pkl', 'w') cPickle.dump(lala, fileo) fileo.close() fileo = open('lala.pkl', 'r') serialized_data = fileo.read() serialized_data (lp1\nI1\naI2\naI3\naS'four'\np2\naS'V'\na. fileo.seek(0) recovered = cPickle.load(fileo) recovered [1, 2, 3, 'four', 'V'] See the docs and feel free to ask if there is some part oyu do not understand. Hope it helps! Hugo Mike Haft wrote: Hello, I've been working on a problem and have now sorted most of it (thanks to some help from the list). All the ways of writing data to a file I know keep telling me that lists can't be written to the file. I'm trying to convert data from one set of files into files of a different format. But the easiest way to get the data from the first set of files is in a list(s). So, is there any way to convert lists to strings? Or a way to write lists to a file? Thanks Mike ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists and strings
yes it is... convert list to string: L = [1,2,3] L = [str(x) for x in L] s = string.join(L,' ') print len(s) convert list to a file myF = open(namaFile,w) for s in myList[:-1]: myF.write(str(s)+\n) myF.write(str(myList[len(myList)-1])) myF.close() Cheers, pujo On 11/8/05, Mike Haft [EMAIL PROTECTED] wrote: Hello,I've been working on a problem and have now sorted most of it (thanksto some help from the list). All the ways of writing data to a file I know keep telling me that listscan't be written to the file. I'm trying to convert data from one set offiles into files of a different format. But the easiest way to get the data from the first set of files is in a list(s).So, is there any way to convert lists to strings? Or a way to write liststo a file?ThanksMike___ Tutor maillist-Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists and strings
+++ Hugo Gonz?lez Monteverde [08-11-05 13:13 -0600]: | Hi Mike, | | Converting an (almost)arbitrary object into a string is what the Pickle module does. CPickle is faster. Take | a look into into it in the docs. | Is there a way to dump the varialble in XML format and retrive it? e.g. a=this is string b=1234567890 c={} c['a'] = a c['b'] = b and then dump c. Regards, Shantanoo pgp5hG8KxTdBW.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists and strings
Shantanoo Mahajan schrieb: +++ Hugo Gonz?lez Monteverde [08-11-05 13:13 -0600]: | Hi Mike, | | Converting an (almost)arbitrary object into a string is what the Pickle module does. CPickle is faster. Take | a look into into it in the docs. | Is there a way to dump the varialble in XML format and retrive it? Look for xml_pickle.py in http://gnosis.cx/download/Gnosis_Utils-current.tar.gz and see the article on it here: http://gnosis.cx/publish/programming/xml_matters_1.txt Also, xmlrpclib.py from the Standard library contains functions to serialize basic Python data types to XML according to the XML-RPC specification. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists and strings
Mike Haft wrote: All the ways of writing data to a file I know keep telling me that lists can't be written to the file. I'm trying to convert data from one set of files into files of a different format. But the easiest way to get the data from the first set of files is in a list(s). So, is there any way to convert lists to strings? Or a way to write lists to a file? What do you want the data to look like in the file? You can create a string from your list and write the string to the file. For example if the list contains strings and you just want to separate the values with spaces use join(): data = ['22.5', '0.3', '11.9'] ' '.join(data) '22.5 0.3 11.9' Or you could separate the values with comma and space: ', '.join(data) '22.5, 0.3, 11.9' I think from your previous post that your actual data is a list of lists, so you have to iterate the outside list, formatting each line and writing it to the file, something like this (borrowing from your previous unanswered post): out_file = open(test.txt,w) data = readSOMNETM(filename) for line in data: line = ' '.join(line) out_file.write(line) out_file.write('\n') # need a newline after each line out_file.close() Kent -- http://www.kentsjohnson.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor