0

The result below for x.split() is what I want because there is no unicode in the result.

server:~ brian$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 'M Y'
>>> x.split()
['M', 'Y']

But in my application using db.get as below, the result for choices is [u'M', u'Y'] instead, when 'choices' = 'M Y'.

def post(self, blog_id):
        blog = db.get(db.Key.from_path('Blogs', blog_id))
    n = Reminders(parent=blog, purpose=self.request.get('purpose'),
                  choices=self.request.get('choices').split())
        n.put()

How can I get my desired result?

This question is different from the other one because it seems as if google-app-engine or python2.7 forces the u'' around each element of the sequence that is split. I have tried using .encode('ascii','ignore') also, but to no avail.

zerowords
  • 2,915
  • 4
  • 29
  • 44
  • 1
    Possible duplicate of [Python .split() without 'u](http://stackoverflow.com/questions/14664209/python-split-without-u) – Delgan May 16 '16 at 17:38
  • How does that affect your program?...as I believe it is advised to deal with unicode strings, and since you are dealing with Python2.7, it is natural that you get `u'M'` and `u'Y'` as output...as they were treaded as unicode string in your `db` objects. – Iron Fist May 16 '16 at 18:06
  • And it seems to me you are working with GAE DB?...am I wrong? – Iron Fist May 16 '16 at 18:08
  • It affects my program because I want to include the results as one of the parameters/arguments to a javascript function. The js function chokes on that argument. I have been [fiddling with it here](https://jsfiddle.net/sqtqtkxb/9/) If you go there, try double clicking on the "M". Then change the input to the function to include u'. – zerowords May 16 '16 at 19:05

2 Answers2

0

Try to use the encode function.

Example:

In [1]: x= u'M Y'

In [2]: x
Out[2]: u'M Y'

In [3]: x.split()
Out[3]: [u'M', u'Y']

In [4]: x.encode('utf8').split()
Out[4]: ['M', 'Y']

In your code, add the encode function before you split:

n = Reminders(parent=blog, purpose=self.request.get('purpose'),
     choices=self.request.get('choices').encode('utf8').split())
lsxliron
  • 540
  • 5
  • 11
  • That looked promising, but would not work. I even tried placing `.encode('utf8')` differently or with more parens, but either get the same result which still includes `u'`, or get an error message that lists do not have .split()s. – zerowords May 16 '16 at 19:00
  • In that case, you might want to do something like `n=[s.encode('utf8') for s in Reminders(parent=blog, purpose=self.request.get('purpose'), choices=self.request.get('choices').encode('utf8').split())]` which will take the previous unicode list and will create a new one without the `u` – lsxliron May 16 '16 at 20:04
  • I am getting the error `TypeError: 'Reminders' object is not iterable`. – zerowords May 16 '16 at 23:36
  • 1
    It seems like Reminders is a class that you implemented yourself. In that case, you have to make it iterable. In order to do this you should implement the `__iter__` function and the `next` function. For more information, take look on the answer to [this question](http://stackoverflow.com/questions/19151/how-to-make-class-iterable) – lsxliron May 17 '16 at 04:08
  • That sounds like it might be a good alternative. If you look at the comments for the question itself, Iron Fist suggested that Reminders is part of GAE's db models. Although I neglected to confirm Iron Fist's "suggestion" it was correct and my code includes `class Reminders(db.Model):`. So I am not sure that it is possible to implement the `__iter__` function in this case. Anyway, for now, as you can tell by seeing my "answer", I have answered the question by dodging it altogether. – zerowords May 17 '16 at 16:09
  • An alternative would be to do this process in two steps. The first step is to get the reminders the way you did. Seems like it returns a list of unicode strings (`n` in your original code). String is an utterable object so you can generate another list without the unicode part: `lst = [s for s in n]` – lsxliron May 17 '16 at 16:46
0

In my case the answer was simple, just do the split() in javascript instead of in python. That way the u'...'s never get generated.

zerowords
  • 2,915
  • 4
  • 29
  • 44