python读取特定字符串以及python格式化

2023-12-29 15:25:13

一、python格式化

命令之间用,隔开,文件格式化需要保存一次才能生效

VSCode最新格式化Python文件的方法_vscode python 格式化-CSDN博客

二、 找字符串

import yaml   
yamlpath = "XXXX.yaml"
get_yaml_data(yamlpath)
def get_yaml_data(yaml_file):
    print("*****获取yaml数据*****")
    with open(yaml_file, encoding="utf-8") as file:
        content = file.read()
        find_str = "XXXXX"
        f_start = content.find(find_str)
        f_end = f_start + len(find_str)
        find_str_2 = "XXX"
        f_start_2 = content.find(find_str_2, f_end)#防止px包含x这种情况出现
        f_end_2 = f_start_2 + len(find_str_2)
        # print('f_start_2',f_start_2)
        # print('f_end_2',f_end_2)
        find_str_3 = "YYYYYY"#找个结束的位置
        f_start_3 = content.find(find_str_3, f_end_2)
        # dic_A = str_to_dic(content[f_end:f_start_2])
        # dic_P = str_to_dic(content[f_end_2:f_start_3])
        # print(dic_A)
        # print(dic_P)
        print(content[f_end:f_start_2])
        print(content[f_end_2:f_start_3])

三、按行读

def get_yaml_data(yaml_file):
    print("*****获取yaml数据*****")
    with open(yaml_file, encoding="utf-8") as file:
        for line in file:
            print(line)

文章来源:https://blog.csdn.net/drivenzyw/article/details/135285933
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。