0

I want to set an encode and BOM to a file.

I want to get a flag of encoding and BOM I set when I saved a file. But both of the two are not saved.

What is wrong with my coding?

I don't know about the Encoding and BOM well, how should I understand this result?

In the case of Utf16, and setAutoDetectUnicode(True). I set the encoding to Utf16, but the read file returns utf-8.

>>> from PySide6.QtCore import QTextStream, QStringConverter, QFile, QSaveFile
>>> file = QFile("t.txt")
>>> file.open(QFile.WriteOnly)
True
>>> out = QTextStream(file)
>>> out.setEncoding(QStringConverter.Encoding.Utf16)
>>> out << "text"
<PySide6.QtCore.QTextStream object at 0x0000011D5E912C40>
>>> file.close()
>>> file = QFile("t.txt")
>>> file.open(QFile.ReadOnly)
True
>>> out = QTextStream(file)
>>> out.setAutoDetectUnicode(True)
>>> out.readAll()
't\x00e\x00x\x00t\x00'
>>> out.encoding()
<Encoding.Utf8: 0>
>>> file.close()

I set the BOM to True, but the result has no BOM.

byteOrderMark

>>> out = QTextStream(file)
>>> out << "text"
<PySide6.QtCore.QTextStream object at 0x0000011D5E8EB700>
>>> out.setEncoding(QStringConverter.Encoding.Utf8)
>>> out.setGenerateByteOrderMark(True)
>>> file.close()
>>> file = QFile("t.txt")
>>> file.open(QFile.ReadOnly)
True
>>> out = QTextStream(file)
>>> out.encoding()
<Encoding.Utf8: 0>
>>> out.generateByteOrderMark()
False
>>> out.readAll()
'text'
>>> file.close()

>>> file = QFile("t.txt")
>>> file.open(QFile.ReadOnly)
True
>>> out = QTextStream(file)
>>> out.setAutoDetectUnicode(True)
>>> out.encoding()
<Encoding.Utf8: 0>
>>> out.generateByteOrderMark()
False
>>> out.readAll()
'text'
>>> file.close()
Haru
  • 1,884
  • 2
  • 12
  • 30
  • The first case seems a bug (but the documentation may seem ok with it). UTF-16 must always have BOM, but Q doesn't write default BOM (without BOM it is Utf16BE and Utf16LE. The second case, I'm not sure. One should never create a UTF-8 file with BOM (but if you are Microsoft and you like to create problems to the other team). Maybe you should check externally if the BOM exists. Or add it manually: write the first char as QChar(0xFEFF) – Giacomo Catenazzi Nov 25 '22 at 07:18

0 Answers0