python 请求头解析分割脚本

190 阅读1分钟
import os

def sync(list):
    values = ""
    for value in list:
        values += value
    return values.replace('\n', '').replace(' ', '').replace('"', '\\"')

if __name__ == '__main__':
    file = input("输入文件:")
    type = input("输出样式:") + '\n'
    out_file = "{}\\temp.txt".format(os.getcwd())
    lines = open(file, 'r').readlines()
    headers = ""
    for line in lines:
        header = line.split(':')
        if (header[0] == ''):
            value = type.format(header[1], sync(header[2:]))
        else:
            value = type.format(header[0], sync(header[1:]))
        headers += value
    open(out_file, 'w+').write(headers)
    os.startfile(out_file)

输入需要解析的请求头所在的文件路径;例如:D:\1.txt image.png 输入输出的格式;例如:.addHeader("{}","{}");需要注意的是,格式字段中必须包含两组{} image.png 程序运行结束会自动打开输出文件 image.png