0

text.txt file includes letters like "ğ", "ü", "ş",(these are Turkish letters). When I print the content, the result is ���

Here is my code in python:

# -*- coding: utf-8 -*-
with open("text.txt") as file:
    contents=file.read()
print(contents)

output:

���

How can I solve this problem? Please let me know and thanks for your time.

I tried different encodes, utf-8, windows1254, etc. I tried firstly to decode and then encode but the letters were deleted. I think the problem is about VSCode because when I used PyCharm, it is OK. Turkish characters are not problem.

Additionally; i tried "monospace" font, it didn't work

when i tried; 'with open("text.txt", encoding="utf-8") as file:' it gives an error: 'UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 25: invalid start byte'

EmreEmre
  • 1
  • 1
  • Try using `monospace` font that supports Turkish characters. See also here: https://stackoverflow.com/questions/72380887/how-do-i-enable-turkish-letters-in-vscode. – LoukasPap Jun 09 '23 at 20:51
  • 1
    The `coding` line declares the encoding of the source file. Use `open("text.txt", encoding="utf8")` to declare the encoding of the file you are reading. Use the actual encoding of the file if it isn’t UTF-8. – Mark Tolonen Jun 09 '23 at 21:04
  • i tried monospace and encoding="utf8" but the problem is not solved. – EmreEmre Jun 10 '23 at 06:13
  • Please [edit] your question to improve your [mcve]. In particular, share hexadecimal dump of the file. – JosefZ Jun 11 '23 at 20:14

1 Answers1

0

The vscode integrated terminal uses the system's built-in terminal, so it prints characters and displays related to many settings, such as your system language, terminal code page, vscode display language and so on.

Workaround

Use Code Runner extension. After installing the extension, use Run Code (Ctrl+Alt+N) to execute the script, and the code result will be display in the OUTPUT panel.

enter image description here

JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • Thanks for answer, İ've already used Code Runner, i reinstalled it, unfortunately it didnt solve the problem. i just write print("ü"), output: SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xfc in position 0: invalid start byte – EmreEmre Jun 12 '23 at 13:26
  • What if you use the code in the question? Read txt text content and then output. – JialeDu Jun 13 '23 at 01:16
  • text.txt: "ü", "ğ" ; output: "�", "�" – EmreEmre Jun 13 '23 at 18:13