zoukankan      html  css  js  c++  java
  • Python读取txt(.ini)文件BOM问题

    2018-06-13   11:20:40

    在windows上使用open打开utf-8编码的txt文件时开头会有一个多余的字符,它叫BOM,是用来声明编码等信息的,但python会把它当作文本解析

    解决办法:open的encoding参数

    1.创建config.ini配置文件

    [DATABASE]
    host = 50.23.190.57
    username = xxxxxx
    password = ******
    port = 3306
    database = databasename
    
    [HTTP]
    # 接口的url
    baseurl = https://xxxxxxxxx
    port = 8080
    timeout = 1.0
    
    [EMAIL]
    mail_host = smtp.163.com
    mail_user = xxx@163.com
    mail_pass = *********
    mail_port = 25
    sender = xxx@163.com
    receiver = xxxx@qq.com/xxxx@qq.com
    subject = python
    content = "All interface test has been complited
    please read the report file about the detile of result in the attachment."
    testuser = Someone
    on_off = 1

    2.读取config.ini配置文件

    import os
    import codecs
    import configparser
    
    #获取当前文件__file__的所在目录
    proDir = os.path.split(os.path.realpath(__file__))[0]
    #获取文件 cfg.ini 的地址
    configPath = os.path.join(proDir, "config.ini")
    print(configPath)
    
    class ReadConfig:
        def __init__(self):
            self.cf = configparser.ConfigParser()
            self.cf.read(configPath) 
    
        def get_email(self, name):
            value = self.cf.get("EMAIL", name)
            return value
    
        def get_http(self,name):
            value = self.cf.get("HTTP", name)
            print(value)
            return value
    
        def get_db(self, name):
            value = self.cf.get("DATABASE", name)
            return value    

    运行结果报错:

    在首行添加   #coding=gbk,其余代码均不变

    #coding=gbk  固定编码格式为 gbk
    import os
    import codecs
    import configparser

    运行结果报错:

    用encoding参数读取文件

    #coding=gbk
    import os
    import codecs
    import configparser
    
    #获取当前文件__file__的所在目录
    proDir = os.path.split(os.path.realpath(__file__))[0]
    #获取文件 cfg.ini 的地址
    configPath = os.path.join(proDir, "config.ini")
    print(configPath)
    
    class ReadConfig:
        def __init__(self):
            self.cf = configparser.ConfigParser()
            self.cf.read(configPath,encoding='utf-8') #encoding='utf_8_sig'

    运行结果成功:

    3.utf-8 与 utf-8-sig两种编码格式的区别

    UTF-8以字节为编码单元,它的字节顺序在所有系统中都是一様的,没有字节序的问题,也因此它实际上并不需要BOM(“ByteOrder Mark”), 但是UTF-8 with BOM即utf-8-sig需要提供BOM("ByteOrder Mark")

    这里我不是很理解,反正在这里无论用那种编码格式都可以

    self.cf.read(configPath,encoding='utf-8') #encoding='utf_8_sig'
    # 执行zip压缩命令,将apitest目录下所有文件打包压缩
    source =[back_dir,back_file]
    target_file=target_dir+time.strftime("%Y%m%d%H%M%S")+'.zip'
    zip_commond ="zip -qr "%s" "%s""%(target_file,''.join(source))
    print(zip_commond)
    if os.system(zip_commond)==0:
    print('Successful backup to',target_file)
    else:
    print('Backup Failed')



  • 相关阅读:
    .Net在arraylist用法
    ORM查询方法
    正则表达式大全
    checkbox修改功能保存功能绑定
    Web ASP.Net运行机制
    面试题
    Sql Server 中锁的概念
    由nginx和spring boot中tomcat配置不当引起的问题
    记录一次由事务可重复读引起的问题
    Ubuntu18.04没有声音的解决
  • 原文地址:https://www.cnblogs.com/jasmine0627/p/9176762.html
Copyright © 2011-2022 走看看