本文整理汇总了Python中PyQt5.QtCore.QTextStream.setCodec方法的典型用法代码示例。如果您正苦于以下问题:Python QTextStream.setCodec方法的具体用法?Python QTextStream.setCodec怎么用?Python QTextStream.setCodec使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtCore.QTextStream
的用法示例。
在下文中一共展示了QTextStream.setCodec方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def save(self):
if "Unnamed" in self.filename:
filename = QFileDialog.getSaveFileName(self,
"G.R.O.M. Editor -- Save File As", self.filename,
"MD files (*.mdp *.itp *.top *.*)")
print('filename is ', filename)
if len(filename[0]) == 0:
return
self.filename = filename[0]
self.setWindowTitle(QFileInfo(self.filename).fileName())
exception = None
fh = None
try:
fh = QFile(self.filename)
if not fh.open(QIODevice.WriteOnly):
raise IOError(str(fh.errorString()))
stream = QTextStream(fh)
stream.setCodec("UTF-8")
stream << self.toPlainText()
self.document().setModified(False)
except EnvironmentError as e:
exception = e
finally:
if fh is not None:
fh.close()
if exception is not None:
raise exception
示例2: readTextFromFile
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def readTextFromFile(self, fileName=None, encoding=None):
previousFileName = self._fileName
if fileName:
self._fileName = fileName
# Only try to detect encoding if it is not specified
if encoding is None and globalSettings.detectEncoding:
encoding = self.detectFileEncoding(self._fileName)
# TODO: why do we open the file twice: for detecting encoding
# and for actual read? Can we open it just once?
openfile = QFile(self._fileName)
openfile.open(QFile.ReadOnly)
stream = QTextStream(openfile)
encoding = encoding or globalSettings.defaultCodec
if encoding:
stream.setCodec(encoding)
# If encoding is specified or detected, we should save the file with
# the same encoding
self.editBox.document().setProperty("encoding", encoding)
text = stream.readAll()
openfile.close()
self.editBox.setPlainText(text)
self.editBox.document().setModified(False)
if previousFileName != self._fileName:
self.updateActiveMarkupClass()
self.fileNameChanged.emit()
示例3: save
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def save(self, content, path=None):
"""
Write a temporary file with .tnj extension and copy it over the
original one.
.nsf = Ninja Swap File
# FIXME: Where to locate addExtension, does not fit here
"""
new_path = False
if path:
self.attach_to_path(path)
new_path = True
save_path = self._file_path
if not save_path:
raise NinjaNoFileNameException("I am asked to write a "
"file but no one told me where")
swap_save_path = "%s.nsp" % save_path
# If we have a file system watcher, remove the file path
# from its watch list until we are done making changes.
if self.__watcher is not None:
self.__watcher.removePath(save_path)
flags = QIODevice.WriteOnly | QIODevice.Truncate
f = QFile(swap_save_path)
if settings.use_platform_specific_eol():
flags |= QIODevice.Text
if not f.open(flags):
raise NinjaIOException(f.errorString())
stream = QTextStream(f)
encoding = get_file_encoding(content)
if encoding:
stream.setCodec(encoding)
encoded_stream = stream.codec().fromUnicode(content)
f.write(encoded_stream)
f.flush()
f.close()
# SIGNAL: Will save (temp, definitive) to warn folder to do something
self.willSave.emit(swap_save_path, save_path)
self.__mtime = os.path.getmtime(swap_save_path)
shutil.move(swap_save_path, save_path)
self.reset_state()
# If we have a file system watcher, add the saved path back
# to its watch list, otherwise create a watcher and start
# watching
if self.__watcher is not None:
if new_path:
# self.__watcher.removePath(self.__watcher.files()[0])
self.__watcher.addPath(self._file_path)
else:
self.__watcher.addPath(save_path)
else:
self.start_watching()
return self
示例4: readFile
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def readFile(self, path, coding = "UTF-8"):
"""读取文件"""
file = QFile(path)
file.open(QIODevice.ReadOnly | QIODevice.Text)
fin = QTextStream(file)
fin.setCodec(coding)
data = fin.readAll()
file.close()
return data
示例5: updateTextEdit
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def updateTextEdit(self):
mib = self.encodingComboBox.itemData(self.encodingComboBox.currentIndex())
codec = QTextCodec.codecForMib(mib)
data = QTextStream(self.encodedData)
data.setAutoDetectUnicode(False)
data.setCodec(codec)
self.decodedStr = data.readAll()
self.textEdit.setPlainText(self.decodedStr)
示例6: loadFile
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def loadFile(self):
fh = QFile(self.filename)
print("fh is ", fh)
if not fh.open(QIODevice.ReadOnly):
raise IOError(str(fh.errorString()))
stream = QTextStream(fh)
stream.setCodec("UTF-8")
# self.setPlainText("Hello World")
self.preParse = (stream.readAll()) # Here lies the problem how to fix it? PyQt 3.4.2 Works Fine
print(self.preParse)
self.parseOutputData(self.preParse)
示例7: saveFileCore
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def saveFileCore(self, fn):
self.fileSystemWatcher.removePath(fn)
savefile = QFile(fn)
result = savefile.open(QIODevice.WriteOnly)
if result:
savestream = QTextStream(savefile)
if globalSettings.defaultCodec:
savestream.setCodec(globalSettings.defaultCodec)
savestream << self.editBoxes[self.ind].toPlainText()
savefile.close()
self.fileSystemWatcher.addPath(fn)
return result
示例8: writeTextToFile
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def writeTextToFile(self, fileName=None):
# Just writes the text to file, without any changes to tab object
# Used directly for i.e. export extensions
savefile = QFile(fileName or self._fileName)
result = savefile.open(QFile.WriteOnly)
if result:
savestream = QTextStream(savefile)
if globalSettings.defaultCodec:
savestream.setCodec(globalSettings.defaultCodec)
savestream << self.editBox.toPlainText()
savefile.close()
return result
示例9: read
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def read(self):
""" Reads the file and returns the content """
_file = QFile(self.filename)
if not _file.open(QIODevice.ReadOnly | QIODevice.Text):
raise Exception(_file.errorString())
# Codec
codec = QTextCodec.codecForLocale()
stream = QTextStream(_file)
stream.setCodec(codec)
return stream.readAll()
示例10: readTextFromFile
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def readTextFromFile(self, encoding=None):
openfile = QFile(self.fileName)
openfile.open(QFile.ReadOnly)
stream = QTextStream(openfile)
encoding = encoding or globalSettings.defaultCodec
if encoding:
stream.setCodec(encoding)
text = stream.readAll()
openfile.close()
markupClass = get_markup_for_file_name(self.fileName, return_class=True)
self.setMarkupClass(markupClass)
modified = bool(encoding) and (self.editBox.toPlainText() != text)
self.editBox.setPlainText(text)
self.editBox.document().setModified(modified)
示例11: save
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def save(self):
fileName, _ = QFileDialog.getSaveFileName(self)
if fileName:
outFile = QFile(fileName)
if not outFile.open(QFile.WriteOnly|QFile.Text):
QMessageBox.warning(self, "Codecs",
"Cannot write file %s:\n%s" % (fileName, outFile.errorString()))
return
action = self.sender()
codecName = action.data()
out = QTextStream(outFile)
out.setCodec(codecName)
out << self.textEdit.toPlainText()
示例12: saveTextToFile
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def saveTextToFile(self, fileName=None, addToWatcher=True):
if fileName is None:
fileName = self.fileName
self.p.fileSystemWatcher.removePath(fileName)
savefile = QFile(fileName)
result = savefile.open(QFile.WriteOnly)
if result:
savestream = QTextStream(savefile)
if globalSettings.defaultCodec:
savestream.setCodec(globalSettings.defaultCodec)
savestream << self.editBox.toPlainText()
savefile.close()
if result and addToWatcher:
self.p.fileSystemWatcher.addPath(fileName)
return result
示例13: saveHtml
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def saveHtml(self, fileName):
if not QFileInfo(fileName).suffix():
fileName += ".html"
try:
htmltext = self.currentTab.getHtml(includeStyleSheet=False,
webenv=True)
except Exception:
return self.printError()
htmlFile = QFile(fileName)
htmlFile.open(QIODevice.WriteOnly)
html = QTextStream(htmlFile)
if globalSettings.defaultCodec:
html.setCodec(globalSettings.defaultCodec)
html << htmltext
htmlFile.close()
示例14: __init__
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def __init__(self, subtitle, encoding="UTF-8"):
super().__init__()
subtitlefile = QFile(subtitle)
if not subtitlefile.open(QIODevice.ReadOnly | QIODevice.Text):
return
text = QTextStream(subtitlefile)
text.setCodec(encoding)
subtitletext = text.readAll()
# ('sıra', 'saat', 'dakika', 'saniye', 'milisaniye', 'saat', 'dakika', 'saniye', 'milisaniye', 'birincisatır', 'ikincisatır')
compile = re.compile(r"(\d.*)\n(\d{2}):(\d{2}):(\d{2}),(\d{3}) --> (\d{2}):(\d{2}):(\d{2}),(\d{3})\n(\W.*|\w.*)\n(\w*|\W*|\w.*|\W.*)\n")
self.sublist = compile.findall(subtitletext)
示例15: writeTextToFile
# 需要导入模块: from PyQt5.QtCore import QTextStream [as 别名]
# 或者: from PyQt5.QtCore.QTextStream import setCodec [as 别名]
def writeTextToFile(self, fileName=None):
# Just writes the text to file, without any changes to tab object
# Used directly for i.e. export extensions
savefile = QFile(fileName or self._fileName)
result = savefile.open(QFile.WriteOnly)
if result:
savestream = QTextStream(savefile)
# Save the file with original encoding
encoding = self.editBox.document().property("encoding")
if encoding is not None:
savestream.setCodec(encoding)
savestream << self.editBox.toPlainText()
savefile.close()
return result