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()