首页 > 编程语言 >Python文本处理

Python文本处理

时间:2023-05-07 21:56:46浏览次数:38  
标签:name Python type 文本处理 file print output data

 

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)

 

标签:name,Python,type,文本处理,file,print,output,data
From: https://www.cnblogs.com/sinferwu/p/17380238.html

相关文章

  • 工作提效___python实现测试用例统计
    一、工作中存在的问题:1、被测项目不断迭代增加新功能,功能模块越来越多,用例采用excel文档进行记录,每个sheet代表一级功能模块,每个sheet里面会有多个二级功能模块。由于功能模块较多,导致测试用例文档中存在几十个sheet页2、由于项目测试中,很多测试用例可以共用一条测试用例,为了减......
  • Python wordpress-xmlrpc错误:xml.parsers.expat.ExpatError: XML or text declaration
    解决方法:修改打开client.py文件原代码:deffeed(self,data):self._parser.Parse(data,0)改成如下的代码:deffeed(self,data):self._parser.Parse(data.strip(),0)......
  • Python neopixel package bugs All In One
    PythonneopixelpackagebugsAllInOnePython3Pythonneopixellibrarydocshttps://docs.circuitpython.org/projects/neopixel/en/latest/index.htmlhttps://github.com/adafruit/Adafruit_CircuitPython_NeoPixelbugs❌https://github.com/adafruit/Adafruit......
  • python入门
    引入我们学习python语言是为了控制计算机、让计算机能够像人一样去工作,所以在python这门语言中,所有语法存在的意义都是为了让计算机具备人的某一项技能,这句话是我们理解后续所有python语法的根本。变量什么是变量#变量就是可以变化的量,量指的是事物的状态,比如人的年龄、性别,......
  • python实现计算器
    python两行代码实现计算器python是一门强大的语言,有时候解决一些问题用python会特别简单,python总会有一些独特的解法,让人出乎意料,还记得初学C语言的时候,一本书上讲解了一个计算器的实现,几十行代码,那时候对于刚刚入坑的我来说几十行代码就已经让我晕头转向了,研究了好久,总算是把它......
  • python3 基本语法
    注释Python中单行注释以#开头多行注释可以用多个#号,还有'''和"""#!/usr/bin/python3#第一个注释#第二个注释'''第三注释第四注释'''"""第五注释第六注释"""print("Hello,Python!")数字类型py......
  • Python数据分析与挖掘实战笔记
    (声明:这些代码只是看书的时候跟着敲一敲,留个印象,为的是以后用到有个方便快速查找看个思路,并没有真正运行。)数据挖掘建模过程数据挖掘建模过程:定义挖掘目标:明确挖掘目标,弄清用户需求。数据采样:采样标准(相关性、可靠性、有效性)采样方法:随机、分层、等距数据探索:进行探索......
  • 常用的Python开发工具比较
    前言​ Python是一种功能强大且易于学习的编程语言,被广泛应用于数据科学、机器学习、Web开发等领域。随着Python在各个领域的应用越来越广泛,越来越多的Python开发工具也涌现出来。但是,对于新手来说,选择一款合适的Python开发工具可能是一件令人困惑的事情。因此,在本文中,我将介绍......
  • python3 pandas
    Pandas教程|菜鸟教程(runoob.com)1、介绍Pandas是一个开放源码、BSD许可的库,提供高性能、易于使用的数据结构和数据分析工具。Pandas可以从各种文件格式比如CSV、JSON、SQL、MicrosoftExcel导入数据。2、Series类这是一个一维数据对象3、DataFrame是一个表格型的......
  • Python的魔术方法,装饰器和属性
    这里将介绍python的所谓魔法方法以及装饰器魔术方法一般在类中以双下划线包围的方法就是魔术方法,或者叫特殊方法。简单来说,Python的魔术方法是为了利用Python的标准方法以及不用去记住标准操作的名称,实现更统一的接口。例如下面的代码importcollectionsCard=collection......