Re: How do I process this?
On 4/03/2014 2:03 PM, Igor Korot wrote: Hi, ALL, I have a csv file which depending on how it was produced gives 2 different strings as shown in the example below (test1 and test2). I am only interested in the first field in test1 and obviously in the whole string of test2. test1 = a test2 = a Try using the csv module: from StringIO import StringIO csvfile = StringIO() csvfile.write('a\n') csvfile.write('a\n') csvfile.seek(0) import csv reader = csv.reader(csvfile) [x[0] for x in reader] ['a', 'a'] -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I process this?
On 2014-03-03 20:03, Igor Korot wrote: Hi, ALL, I have a csv file which depending on how it was produced gives 2 different strings as shown in the example below (test1 and test2). I am only interested in the first field in test1 and obviously in the whole string of test2. So, I tried to see if I can get what I want in one simple way, but failed. Is it possible to process the file independently? Thank you. test1 = a test2 = a (t1,_) = test1.split(',') Traceback (most recent call last): File stdin, line 1, in module ValueError: too many values to unpack (t2,_) = test2.split(',') Traceback (most recent call last): File stdin, line 1, in module ValueError: need more than 1 value to unpack Have you tried t = test.split(',', 1)[0] or t = test.partition(',')[0] Both of which yield the same results for me. -tkc -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I process this?
On Mon, Mar 3, 2014 at 10:03 PM, Igor Korot ikoro...@gmail.com wrote: Hi, ALL, I have a csv file which depending on how it was produced gives 2 different strings as shown in the example below (test1 and test2). I am only interested in the first field in test1 and obviously in the whole string of test2. So, I tried to see if I can get what I want in one simple way, but failed. Is it possible to process the file independently? Thank you. python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. test1 = a test2 = a (t1,_) = test1.split(',') Traceback (most recent call last): File stdin, line 1, in module ValueError: too many values to unpack (t2,_) = test2.split(',') Traceback (most recent call last): File stdin, line 1, in module ValueError: need more than 1 value to unpack I'm not sure what you mean by is it possible to process the file independently, can you expand on that a bit? As far as what you're trying to do, there are a couple ways to do it. If you can use Python 3 instead: T:\emppy -3 Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)] on win32 Type help, copyright, credits or license for more information. test1 = a test2 = a t1, *remainder1 = test1.split(',') t1 'a' remainder1 ['', '', '', ''] t2, *remainder2 = test2.split(',') t2 'a' remainder2 [] ^Z Or, if you're stuck on Python 2: T:\emppy -2 Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32 Type help, copyright, credits or license for more information. test1 = a test2 = a t1 = test1.split(',')[0] t1 'a' t2 = test2.split(',')[0] t2 'a' Hope this helps, -- Zach -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I process this?
In mailman.7690.1393905834.18130.python-l...@python.org Igor Korot ikoro...@gmail.com writes: --047d7b6dc250a1426004f3bffd8d Content-Type: text/plain; charset=ISO-8859-1 Hi, ALL, I have a csv file which depending on how it was produced gives 2 different strings as shown in the example below (test1 and test2). I am only interested in the first field in test1 and obviously in the whole string of test2. So, I tried to see if I can get what I want in one simple way, but failed. Is it possible to process the file independently? Thank you. split() returns a list. Use indexing to grab only the element you want, like this: t1 = test1.split(',')[0] t2 = test2.split(',')[0] -- John Gordon Imagine what it must be like for a real medical doctor to gor...@panix.comwatch 'House', or a real serial killer to watch 'Dexter'. -- https://mail.python.org/mailman/listinfo/python-list
Re: How do I process this using Python?
On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com wrote: I'm writing a processor for Bitmessage messages and I am needing to parse the following returned JSON string: {u'inboxMessages': Does the JSON string really have those u prefixes and apostrophes? That's not valid JSON. You may be able to use ast.literal_eval() on it - I was able to with the example data - but not a JSON parser. Can you sort out your transmission end? ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I process this using Python?
On 08/31/2013 06:48 PM, Chris Angelico wrote: On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com wrote: I'm writing a processor for Bitmessage messages and I am needing to parse the following returned JSON string: {u'inboxMessages': Does the JSON string really have those u prefixes and apostrophes? That's not valid JSON. You may be able to use ast.literal_eval() on it - I was able to with the example data - but not a JSON parser. Can you sort out your transmission end? ChrisA I think I remembered what the 'u' prefix is. It indicates that the data following is a unicode string. So could that be valid JSON data wrapped up in unicode? Anthony -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I process this using Python?
On Sun, Sep 1, 2013 at 10:19 AM, Anthony Papillion papill...@gmail.com wrote: On 08/31/2013 06:48 PM, Chris Angelico wrote: On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com wrote: I'm writing a processor for Bitmessage messages and I am needing to parse the following returned JSON string: {u'inboxMessages': Does the JSON string really have those u prefixes and apostrophes? That's not valid JSON. You may be able to use ast.literal_eval() on it - I was able to with the example data - but not a JSON parser. Can you sort out your transmission end? ChrisA I think I remembered what the 'u' prefix is. It indicates that the data following is a unicode string. So could that be valid JSON data wrapped up in unicode? No; JSON already supports Unicode. What you may have is an incorrect JSON encoder that uses Python's repr() to shortcut its work - but it's wrong JSON. But bitmessage seems to be written in Python. Can you simply access the objects it's giving you, rather than going via text strings? ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I process this using Python?
On 31Aug2013 19:19, Anthony Papillion papill...@gmail.com wrote: | On 08/31/2013 06:48 PM, Chris Angelico wrote: | On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com wrote: | I'm writing a processor for Bitmessage messages and I am needing to | parse the following returned JSON string: | | {u'inboxMessages': | | Does the JSON string really have those u prefixes and apostrophes? | That's not valid JSON. You may be able to use ast.literal_eval() on it | - I was able to with the example data - but not a JSON parser. Can you | sort out your transmission end? | | I think I remembered what the 'u' prefix is. It indicates that the data | following is a unicode string. So could that be valid JSON data wrapped | up in unicode? How sure are you that it is JSON? It looks to me like a message that might once have been JSON, but has already been passed through json.loads() for you. What does type(the_json) say? What does repr(the_json) say? Print both. I would guess it has already been parsed. Cheers, -- Cameron Simpson c...@zip.com.au Sattinger's Second Law: Even though you've plugged it in, it still won't work until you switch it on. -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I process this using Python?
On 08/31/2013 07:32 PM, Cameron Simpson wrote: On 31Aug2013 19:19, Anthony Papillion papill...@gmail.com wrote: | On 08/31/2013 06:48 PM, Chris Angelico wrote: | On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com wrote: | I'm writing a processor for Bitmessage messages and I am needing to | parse the following returned JSON string: | | {u'inboxMessages': | | Does the JSON string really have those u prefixes and apostrophes? | That's not valid JSON. You may be able to use ast.literal_eval() on it | - I was able to with the example data - but not a JSON parser. Can you | sort out your transmission end? | | I think I remembered what the 'u' prefix is. It indicates that the data | following is a unicode string. So could that be valid JSON data wrapped | up in unicode? How sure are you that it is JSON? It looks to me like a message that might once have been JSON, but has already been passed through json.loads() for you. What does type(the_json) say? What does repr(the_json) say? Print both. I would guess it has already been parsed. Cheers, And you would be right! It's actually a dictionary! Anthony -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I process this using Python? (SOLVED)
On 08/31/2013 07:32 PM, Chris Angelico wrote: On Sun, Sep 1, 2013 at 10:19 AM, Anthony Papillion papill...@gmail.com wrote: On 08/31/2013 06:48 PM, Chris Angelico wrote: On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion papill...@gmail.com wrote: I'm writing a processor for Bitmessage messages and I am needing to parse the following returned JSON string: {u'inboxMessages': Does the JSON string really have those u prefixes and apostrophes? That's not valid JSON. You may be able to use ast.literal_eval() on it - I was able to with the example data - but not a JSON parser. Can you sort out your transmission end? ChrisA I think I remembered what the 'u' prefix is. It indicates that the data following is a unicode string. So could that be valid JSON data wrapped up in unicode? No; JSON already supports Unicode. What you may have is an incorrect JSON encoder that uses Python's repr() to shortcut its work - but it's wrong JSON. But bitmessage seems to be written in Python. Can you simply access the objects it's giving you, rather than going via text strings? ChrisA Once I looked at the data in a better structured way and saw it in the proper type it became clear. Here is my solution: data = json.loads(api.getAllInboxMessages()) for message in data['inboxmessages']: print message['fromAddress'] Yep, it was that simple. Thanks for the help guys! I appreciate how fast everyone responded. Anthony -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I process this using Python?
On Sat, 31 Aug 2013 18:44:04 -0500, Anthony Papillion wrote: Hello Everyone, I'm writing a processor for Bitmessage messages and I am needing to parse the following returned JSON string: [...] For some reason (probably obvious reasons) isn't working. I'm trying to loop through the JSON and return all of the fromAddress fields. Can anyone offer suggestions? Yes. Don't assume that isn't working is obvious. What do you mean isn't working? What part isn't working? What does it do? Does it print the wrong results, or does it raise an exception? If so, please copy and paste the entire traceback, do not simplify it, summarise it, or retype it from memory. -- Steven -- http://mail.python.org/mailman/listinfo/python-list