首页 > 其他分享 >返回txt文档编码格式

返回txt文档编码格式

时间:2024-05-13 21:41:23浏览次数:12  
标签:编码 encoding 文档 path chardet detector txt

  1. 安装chardet
pip install chardet
  1. 获得文本文档编码类型
import chardet
from chardet.universaldetector import UniversalDetector
def GetEncoding(file_path):
    txt = open(file_path, "rb")
    detector = UniversalDetector()
    for line in txt.readlines():
        detector.feed(line)
        if detector.done:
            break
    detector.close()
    txt.close()
    return detector.result

my_path = '.\\my_test.txt'
f = open(my_path, 'rb')
str1 = f.read()
char_encoding= chardet.detect(str1)
print(f'字符串为:{str1}')
print(f'字符串编码信息为:{char_encoding}' ) 
print(f'字符串编码为: {char_encoding["encoding"]}')

标签:编码,encoding,文档,path,chardet,detector,txt
From: https://www.cnblogs.com/xiacuncun/p/18190092

相关文章

  • GGML 非官方中文文档
    _CRT_SECURE_NO_DEPRECATE类型:宏定义描述:禁用Windows上荒谬的“不安全”警告。_USE_MATH_DEFINES类型:宏定义描述:在MSVC上使用M_PI。GGML_DEBUG类型:宏定义描述:定义调试级别。GGML_GELU_FP16类型:宏定义描述:定义GELU函数的浮点数精度为16位。GGML_GELU_QUICK_FP......
  • PyAlgoTrade-0-20-中文文档-一-
    PyAlgoTrade0.20中文文档(一)介绍原文:gbeced.github.io/pyalgotrade/docs/v0.20/html/intro.htmlPyAlgoTrade是一个支持事件驱动的算法交易Python库,支持:使用来自CSV文件的历史数据进行回测。使用Bitstamp实时数据进行模拟交易。在Bitstamp上进行真实交易。......
  • PyAlgoTrade-0-20-中文文档-四-
    PyAlgoTrade0.20中文文档(四)SMA交叉原文:gbeced.github.io/pyalgotrade/docs/v0.20/html/sample_sma_crossover.html将此代码保存为sma_crossover.py:frompyalgotradeimportstrategyfrompyalgotrade.technicalimportmafrompyalgotrade.technicalimportcrosscl......
  • PyAlgoTrade-0-20-中文文档-三-
    PyAlgoTrade0.20中文文档(三)工具原文:gbeced.github.io/pyalgotrade/docs/v0.20/html/tools.htmlQuandlpyalgotrade.tools.quandl.``build_feed(sourceCode,tableCodes,fromYear,toYear,storage,frequency=86400,timezone=None,skipErrors=False,authToken=None,columnNames={......
  • PyAlgoTrade-0-20-中文文档-二-
    PyAlgoTrade0.20中文文档(二)经纪人-订单管理类原文:gbeced.github.io/pyalgotrade/docs/v0.20/html/broker.html基础模块和类类pyalgotrade.broker.``Order(type_,action,instrument,quantity,instrumentTraits)基类:object订单的基类。参数:type(Order.Type)–订单类......
  • Zipline-3-0-中文文档-一-
    Zipline3.0中文文档(一)原文:zipline.ml4trading.io回测您的交易策略原文:zipline.ml4trading.io/index.htmlZipline是一个用于回测的Pythonic事件驱动系统,由众包投资基金Quantopian开发和使用,作为回测和实时交易引擎。自2020年底关闭以来,托管这些文档的域名已过期。......
  • 【译】使用 GitHub Copilot 作为你的编码 GPS
    GitHubCopilot是一个改变游戏规则的人工智能助手,可以彻底改变您在VisualStudio中的编码流程。在我们的视频系列中,Bruno Capuano探讨了这个智能编码伙伴如何帮助您更有效地编写代码,同时保持质量和准确性。Copilot:是助手,而不是替代品Bruno强调,GitHubCopilot旨......
  • 设计程序,获取当前系统时间,把时间转换为特定格式”yy年mm月dd日 星期x tt:mm:ss”,并每
    /***********************************************************************************************************设计程序,获取当前系统时间,把时间转换为特定格式”yy年mm月dd日星期xtt:mm:ss”,并每隔1s写入到本地磁盘中一个*叫做log.txt的文本中,如果文本不存在则创建。......
  • git文档
    git常用命令。创建SSHKeyssh-keygen-trsa-C"[email protected]"配置用户信息gitconfig--globaluser.name"YourName"gitconfig--globaluser.email"[email protected]"仓库在当前目录新建一个Git代码库gitinit新建一个目录,将其......
  • [附源码+文档]Java Swing小游戏源码合集(14款)_毕业设计必选项目
    (小众游戏塔防迷宫动作剧情类等)16款游戏源码Javaswing五子棋联网版源代码Javaswing贪吃蛇游戏开发教程+源码Javaswing超级玛丽游戏Javaswing俄罗斯方块项目源码Javaswing飞机大战游戏源码Javaswing雷电游戏源码Javaswing连连看游戏源码Javaswing模拟写字板源码......