Python文本处理

 

binascii — Convert between binary and ASCII — Python 3.11.3 documentation

 

Hackbright-challenges/hexconvert.py at master · kritikadusad/Hackbright-challenges · GitHub

 

hex2bin/hex2bin.py at main · jasonalexander-ja/hex2bin (github.com)

import re
import os
import sys
import os.path
import binascii

# gets the Command line arguments, it will print the response if there are not 3 arguments.
def get_command_args():
    if len(sys.argv) != 4:
        print("Please specify 3 arguments \"python hex-dump-conv.py input-file.txt output-file.bin data_type (hex/bin)\"")
        sys.exit()
    return (sys.argv[1], sys.argv[2], sys.argv[3])

# Gets this file name, depending on the datatype being read it will either read it bytes or string
def get_input_file(file_name, data_type):
    output = ""
    try:
        if data_type.lower() == "bin":
            output = open(file_name, "r")
        elif data_type.lower() == "hex":
            output = open(file_name, "rb")
    except:
        print(f"Couldn't open file: {file_name}")
        sys.exit()
    return output

# Creates new file in the specified directory, depending on the data_type.
def get_output_file(file_name, data_type):
    if os.path.isfile(file_name):
        cont = input("The specified output file already exists, continue Y/N: ").capitalize()
        if cont.startswith("N"):
            sys.exit()
    try:
        if data_type.lower() == "bin":
            return open(file_name, "wb")
        elif data_type.lower() == "hex":
            return open(file_name, "w")
    except:
        print(f"Couldn't create file at directory: {file_name}; please ensure this directory exists.")
        sys.exit()

def check_values(file):
    if len(file) % 2 != 0:
        print("File needs to be a multiple of 2, each hex code contains 2 characters")
        sys.exit()
    for pos, val in enumerate(file):
        if not re.search("[A-F]|[0-9]", val):
            print(f"Error at position: {pos}, value: {val}; Hex characters need to be 0-9 or A-F")
            sys.exit()

def format_file(file_in):
    file = file_in.upper()
    to_replace = [" ", "\n","\r","\t","\f","\v"]
    for val in to_replace:
        file = file.replace(val, "")
    return file
# converts the file to binary and will convert the contents accordingly
def parse_input(file_in):
    file = format_file(file_in)
    check_values(file)
    output = []
    for pos in range(0, len(file), 2):
        value_str = f"0x{file[pos]}{file[pos + 1]}"
        output.append(int(value_str, 16))
    return bytearray(output)

def hex_convert(data_in):
    try:
        conv = binascii.hexlify(data_in)
    except:
        print("Unable to convert the binary file to a hex file")
        sys.exit()
    return conv

def main():
    try:
        (input_name, output_name, data_type) = get_command_args()
    except:
        print("please type: python hex2bin.py filelocation output location datatype /n use Bin to convert the file to a binary file, use hex to convert the file to a hexadecimal file.")

    input_file = get_input_file(input_name, data_type)

    if data_type.lower() == "bin":
        data = parse_input(input_file.read())

        output_file = get_output_file(output_name, data_type)
        output_file.write(data)
    elif data_type.lower() == "hex":
        hexdata = hex_convert(input_file.read())

        output_file = get_output_file(output_name+".txt", data_type)
        output_file.write(str(hexdata))
    else:
        print("Invalid parameters")
        sys.exit()

    input_file.close()
    output_file.close()

main()

 

intelhex/bin2hex.py at master · python-intelhex/intelhex · GitHub

develone/bin2hex_hex2bin (github.com)

draganglumac/hex2bin: Python muckabout HEX to binary converter. (github.com)

 

hex2bin.py

import re
import os
import sys
import os.path
import binascii

# gets the Command line arguments, it will print the response if there are not 3 arguments.
def get_command_args():
    if len(sys.argv) != 4:
        print("Please specify 3 arguments \"python hex-dump-conv.py input-file.txt output-file.bin data_type (hex/bin)\"")
        #sys.exit()
    return ("log_0006.txt", "out.bin", "bin")

# Gets this file name, depending on the datatype being read it will either read it bytes or string
def get_input_file(file_name, data_type):
    output = ""
    try:
        if data_type.lower() == "bin":
            output = open(file_name, "r")
        elif data_type.lower() == "hex":
            output = open(file_name, "rb")
    except:
        print(f"Couldn't open file: {file_name}")
        sys.exit()
    return output

# Creates new file in the specified directory, depending on the data_type.
def get_output_file(file_name, data_type):
    if os.path.isfile(file_name):
        cont = input("The specified output file already exists, continue Y/N: ").capitalize()
        if cont.startswith("N"):
            sys.exit()
    try:
        if data_type.lower() == "bin":
            return open(file_name, "wb")
        elif data_type.lower() == "hex":
            return open(file_name, "w")
    except:
        print(f"Couldn't create file at directory: {file_name}; please ensure this directory exists.")
        sys.exit()

def check_values(file):
    print(len(file))
    if len(file) % 2 != 0:
        print("File needs to be a multiple of 2, each hex code contains 2 characters")
        sys.exit()
    for pos, val in enumerate(file):
        if not re.search("[A-F]|[0-9]", val):
            print(f"Error at position: {pos}, value: {val}; Hex characters need to be 0-9 or A-F")
            sys.exit()

def format_file(file_in):
    file = file_in.upper()
    to_replace = [" ", "\n","\r","\t","\f","\v"]
    for val in to_replace:
        file = file.replace(val, "")
    return file
# converts the file to binary and will convert the contents accordingly
def parse_input(file_in):
    file = format_file(file_in)
    check_values(file)
    output = []
    for pos in range(0, len(file), 2):
        value_str = f"0x{file[pos]}{file[pos + 1]}"
        output.append(int(value_str, 16))
    return bytearray(output)

def hex_convert(data_in):
    try:
        conv = binascii.hexlify(data_in)
    except:
        print("Unable to convert the binary file to a hex file")
        sys.exit()
    return conv

def main():
    try:
        (input_name, output_name, data_type) = get_command_args()
    except:
        print("please type: python hex2bin.py filelocation output location datatype /n use Bin to convert the file to a binary file, use hex to convert the file to a hexadecimal file.")

    input_file = get_input_file(input_name, data_type)

    if data_type.lower() == "bin":
        data = parse_input(input_file.read())

        output_file = get_output_file(output_name, data_type)
        output_file.write(data)
    elif data_type.lower() == "hex":
        hexdata = hex_convert(input_file.read())

        output_file = get_output_file(output_name+".txt", data_type)
        output_file.write(str(hexdata))
    else:
        print("Invalid parameters")
        sys.exit()

    input_file.close()
    output_file.close()

main()

 

array2bin.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import sys
import re
import  struct

str = "61 6 0.098 11.541"
print(str.split(" "))
print(list(filter(lambda x: x, str.split(" "))))


# hex = sys.argv[1].lower()
# if re.match('^0?x?[a-f0-9]+$', hex) == None:
#     print("The string {0} is not a hexadecimal number.".format(hex))
#     exit(1)


with open("log.txt", "r") as file_in, open("log.bin", "wb") as file_out:
    line_data = file_in.readline()
    print(line_data.strip())
    while line_data:
        line_data = file_in.readline()
        line_data = line_data.strip()
        print(line_data.strip())

        data_list = line_data.split(",")
        #print(len(data_list))

        data_list_new = list(filter(lambda x:x, line_data.split(",")))
        print(data_list_new)
        data_list_new1 = print(list(filter(None, data_list)))
        print(data_list_new1)

        for idx, value in enumerate(data_list_new):
            print(idx, value)
            int_value = int(value, 16)
            hex_value = hex(int_value)
            pack_data  =  struct.pack( "B", int_value)

            file_out.write(pack_data)

 

posted @ 2023-05-07 21:41  sinferwu  阅读(11)  评论(0编辑  收藏  举报