0

i have below code in python 3.5 screen of IDE and powershell

real_path_log_file = 'C:\\Users\\XXXXX\\Desktop\\report\\file1.log'
with open(real_path_log_file, 'r', encoding='utf-8') as open_log:
    read_log = open_log.readlines()
    print(read_log)

from powershell i get below error but if i run this from pycharm i can getting the output of file content please note that file i am reading has ★ character

Traceback (most recent call last):
  File ".\test_read_file.py", line 5, in <module>
    print(read_log)
  File "C:\Users\XXXXX\AppData\Local\Programs\Python\Python35\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2605' in position 367: character maps to <undefined>
js2010
  • 23,033
  • 6
  • 64
  • 66
  • Are pycharm and powershell using the same instance/install of python? – Chris Doyle Aug 12 '20 at 16:25
  • from pycharm i can see that "C:\Users\XXXXX\AppData\Local\Programs\Python\Python35\python.exe" interpeter is selected. how can i check it from powershell ? – Prakash Kukanoor Aug 12 '20 at 16:48
  • i did below in power shell. Does this help ? >>> import sys >>> sys.path ['', 'C:\\Users\\XXXXX\\AppData\\Local\\Programs\\Python\\Python35\\python35.zip', 'C:\\Users\\XXXXX\\AppData\\Local\\Programs\\Python\\Python35\\DLLs', 'C:\\Users\\XXXXX\\AppData\\Local\\Programs\\Python\\Python35\\lib', 'C:\\Users\\XXXXX\\AppData\\Local\\Programs\\Python\\Python35', 'C:\\Users\\XXXXX\\AppData\\Roaming\\Python\\Python35\\site-packages', 'C:\\Users\\XXXXX\\AppData\\Local\\Programs\\Python\\Python35\\lib\\site-packages', ' – Prakash Kukanoor Aug 12 '20 at 16:58
  • What does `python -V` say in both prompts? That's capital `V`. – js2010 Aug 12 '20 at 18:39
  • its printing Python 3.5.0 – Prakash Kukanoor Aug 12 '20 at 18:53
  • Thank you js2010. yes printing was the issue. hence i started writing the output to text file for my validation and i could see the correct text written to my text file Also below post of the same topic helped me https://stackoverflow.com/questions/13590749/reading-unicode-file-data-with-bom-chars-in-python – Prakash Kukanoor Aug 14 '20 at 14:25

1 Answers1

1

I'm no python expert. At least in 3.5, I think print() needs to believe that the console can support unicode characters. For what it's worth, 3.8.5 doesn't have this problem.

js2010
  • 23,033
  • 6
  • 64
  • 66