0

It seems as though python is changing my key_s string into unicode, and I don't know why. I don't remember this being the normal behavior. If I type the lines manually in the interpreter, I don't have to convert them to strings first. I am not used to " or ' indicating unicode but rather strings. Did something change I'm unaware of? Or does it have something to do with pycharm?

key_s = "time_delta"
print("key_s type:", type(key_s))
key_s = str(key_s)
print("key_s type:", type(key_s))

stdout when running python program.py

key_s type: <type 'unicode'>
key_s type: <type 'str'>

stdout from interpreter when typing key_s = "time_delta"; print("key_s type:", type(key_s))

key_s type: <type 'str'>

It defaults to str here, the behavior I would expect when I run the program. What gives?

I made a quick test.py:

#!/usr/bin/env python -u
# encoding: utf-8
print (type('123'))

outputs: <type 'str'>

Any ideas?

SwimBikeRun
  • 4,192
  • 11
  • 49
  • 85
  • Do you have an encoding declaration at the top of the file? `# -*- coding: utf-8 -*-`? – TemporalWolf Apr 05 '17 at 03:06
  • There is. But deleting it, saving, and re-running doesn't change anything. Should it? – SwimBikeRun Apr 05 '17 at 03:11
  • Show us code that, when run *exactly as posted*, demonstrates the problem, along with the *exact output* of running the *exact code posted*. The code you've posted is clearly missing things; for example, it's using Python 3-style `print` syntax and (supposedly) getting Python 3-like output without a `future` statement. – user2357112 Apr 05 '17 at 03:29
  • 1
    Also, why the heck are you using the `-u` flag in the `type('123')` test? This suggests that you might be using the [`-U` flag](https://docs.python.org/2/using/cmdline.html#cmdoption-U) to run your other code, which is a flag you shouldn't use that would make all string literals default to unicode, pretty much exactly like you're seeing. – user2357112 Apr 05 '17 at 03:32
  • are you using a future import? like `from __future__ import unicode_literals` – Copperfield Apr 05 '17 at 03:32
  • 3
    (`-U` is literally documented under "Options you shouldn’t use", because you shouldn't use it.) – user2357112 Apr 05 '17 at 03:33
  • @user2357112 I didn't realize that was a category of cmdline options for Python. You win my internet for today. Hilarious. – TemporalWolf Apr 05 '17 at 03:52
  • I was trying to use the -u tag to see if that affected the result. But still doesn't – SwimBikeRun Apr 05 '17 at 18:25

0 Answers0