[Tutor] trouble with stringio function in python 3.2

Alan Gauld alan.gauld at btinternet.com
Mon May 4 09:27:57 CEST 2015


On 04/05/15 06:03, anupama srinivas murthy wrote:
> 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))

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.

> 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

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




More information about the Tutor mailing list