Discussion:
[Tutor] trouble with stringio function in python 3.2
anupama srinivas murthy
2015-05-04 05:03:34 UTC
Permalink
Hello,

My python code needs to run on versions 2.7 to 3.4. To use stringio
function as appropriate, the code i use is;

if sys.version < '3':
dictionary = io.StringIO(u"""\n""".join(english_words))
else:
dictionary = io.StringIO("""\n""".join(english_words))

The code runs fine on all versions mentioned above except for 3.2 where i
get the error:
dictionary = io.StringIO(u"""\n""".join(english_words))
^
SyntaxError: invalid syntax

How can I solve the issue?

Thank you
Anupama
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-05-04 07:27:57 UTC
Permalink
Post by anupama srinivas murthy
Hello,
My python code needs to run on versions 2.7 to 3.4. To use stringio
function as appropriate, the code i use is;
dictionary = io.StringIO(u"""\n""".join(english_words))
dictionary = io.StringIO("""\n""".join(english_words))
Are you sure you are cutting and pasting the exact code
and error messages?
If so are you using plain text?

Because the indentation of the code above is wrong and should
give an error message every time.
Post by anupama srinivas murthy
The code runs fine on all versions mentioned above except for 3.2 where i
dictionary = io.StringIO(u"""\n""".join(english_words))
^
SyntaxError: invalid syntax
The code you have shown us doesn't run on any version.
We will need to see the exact code and full error message.

Also, have you looked at the string returned by sys.version?
Comparing it to '3' like that is not a reliable way to test
version. You would be better off using sys.version_info.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Chris Warrick
2015-05-04 07:46:22 UTC
Permalink
On Mon, May 4, 2015 at 7:03 AM, anupama srinivas murthy
Post by anupama srinivas murthy
Hello,
My python code needs to run on versions 2.7 to 3.4. To use stringio
function as appropriate, the code i use is;
A better comparison to use would be

if sys.version_info[0] == 2:

It’s the standard idiom, which compares 2 == 2 instead of '2.7.9 (more
garbage here)' < '3'.
Post by anupama srinivas murthy
dictionary = io.StringIO(u"""\n""".join(english_words))
dictionary = io.StringIO("""\n""".join(english_words))
The code runs fine on all versions mentioned above except for 3.2 where i
dictionary = io.StringIO(u"""\n""".join(english_words))
^
SyntaxError: invalid syntax
How can I solve the issue?
The best solution is not supporting Python 3.2, especially considering
that Python 3.3.0 was released in September 2012 (and the latest
version is 3.4.3).

Python 3.0–3.2 do not support the u'' notation for Unicode strings, it
was restored in Python 3.3 to make it easier to write code compatible
with 2.x and 3.x (just like you are trying to do here!) Python has to
read in and parse all the code, and it encounters a strange u'' thing
it does not recognize. Thus, it fails with a SyntaxError.

Many Python projects out there don’t support 3.2, and this was one of
the reasons.

If you REALLY have to support it (why?), there are two solutions. The
first one is to use '\n'.decode('utf-8') instead of u'\n' for Python
2.7, which will not trigger a compile error (it does not matter that
there is no str.decode in Python 3, the syntax makes sense even if it
cannot be executed). The second one is to use (at the top of your
file)

from __future__ import unicode_literals

This will make Python 2.7 think '\n' is a Unicode string, and Python
3.x will ignore this line — in that case, you can even drop the
if/else and just use the same dictionary assignment for both Pythons.
Just be warned that it applies to the entire file and can lead to
problems.

PS. There is no reason to use """multi-line strings""" in this case,
regular "strings" will do it equally well and are more readable.
--
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listin
eryksun
2015-05-04 14:58:05 UTC
Permalink
Post by Chris Warrick
Python 3.0–3.2 do not support the u'' notation for Unicode strings, it
was restored in Python 3.3 to make it easier to write code compatible
with 2.x and 3.x
ur'abc'
File "<stdin>", line 1
ur'abc'
^
SyntaxError: invalid syntax
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinf

Peter Otten
2015-05-04 07:53:16 UTC
Permalink
Post by anupama srinivas murthy
Hello,
My python code needs to run on versions 2.7 to 3.4. To use stringio
function as appropriate, the code i use is;
dictionary = io.StringIO(u"""\n""".join(english_words))
dictionary = io.StringIO("""\n""".join(english_words))
The code runs fine on all versions mentioned above except for 3.2 where i
dictionary = io.StringIO(u"""\n""".join(english_words))
^
SyntaxError: invalid syntax
How can I solve the issue?
Unfortunately Python 3.2 doesn't understand the u-prefixed syntax for
unicode strings. Are the strings in english_words all unicode? Then

dictionary = io.StringIO("\n".join(english_words))

should work. In Python 3 "\n" is unicode anyway, and Python 2 implicitly
converts "\n" to unicode:

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Post by anupama srinivas murthy
"\n".join([u"foo", u"bar"])
u'foo\nbar'


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-05-04 08:07:57 UTC
Permalink
Post by Peter Otten
Post by anupama srinivas murthy
Hello,
My python code needs to run on versions 2.7 to 3.4. To use stringio
function as appropriate, the code i use is;
dictionary = io.StringIO(u"""\n""".join(english_words))
dictionary = io.StringIO("""\n""".join(english_words))
The code runs fine on all versions mentioned above except for 3.2 where i
dictionary = io.StringIO(u"""\n""".join(english_words))
^
SyntaxError: invalid syntax
How can I solve the issue?
Unfortunately Python 3.2 doesn't understand the u-prefixed syntax for
unicode strings. Are the strings in english_words all unicode? Then
dictionary = io.StringIO("\n".join(english_words))
should work. In Python 3 "\n" is unicode anyway, and Python 2 implicitly
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Post by anupama srinivas murthy
"\n".join([u"foo", u"bar"])
u'foo\nbar'
There is also the

from __future__ import unicode_literals

directive in Python 2.7. With that

"..." will be unicode, and bytestrings must be marked b"...".
The latter is understood by both 2.7 and 3.x

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
anupama srinivas murthy
2015-05-04 07:29:36 UTC
Permalink
On May 4, 2015 2:17 AM, "anupama srinivas murthy" <
Post by anupama srinivas murthy
Hello,
My python code needs to run on versions 2.7 to 3.4. To use stringio
function as appropriate, the code i use is;
dictionary = io.StringIO(u"""\n""".join(english_words))
dictionary = io.StringIO("""\n""".join(english_words))
The code runs fine on all versions mentioned above except for 3.2 where i
dictionary = io.StringIO(u"""\n""".join(english_words))
^
SyntaxError: invalid syntax
How can I solve the issue?
Thank you
Anupama
On 4 May 2015 at 12:48, Reuben <***@gmail.com> wrote:

Did you import correct library?


Thank you for asking

Yes. I made a new modification and the code now runs fine on python 3.2 as
well. I replaced;

dictionary = io.StringIO(u"""\n""".join(english_words))

with

dictionary = io.StringIO(unicode('\n').join(english_words))

However, i do not understand why it works fine now
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...