Because of the need to work on a field in the XML file a string of 16 binary number of conversion processing, so write the following two scripts, directly paste the script file content.
#!/usr/bin/python# -*- coding: utf-8 -*-#Function:将userlocation字段按16进制表示的位数分割,每两位一组,用空格隔开#version 1.1#Author: Herman#Date: 2018-06-04#Usage: python fenge.py xmlfile_dir >> tar_dirimport re;import sys;#定义函数def fenge(): alist = [] with open(sys.argv[1], ‘r‘) as f: for line in f: for i in range(0, len(line), 2): alist.append(line[i:i+2]) print ‘ ‘.join(alist) alist = []if __name__=="__main__": fenge();
#!/usr/bin/python# -*- coding: utf-8 -*-#Function:将userlocation字段按16进制表示的位数分割,每两位一组,用空格隔开#version 1.1#Author: Herman#Date: 2018-06-04#Usage: python fenge.py xmlfile_dir >> tar_dirimport re;import sys;def fenge(): with open(sys.argv[1], ‘r‘) as f: for subject in f.readlines(): result = re.sub(r"(?<=\w)(?=(?:\w\w)+$)", " ", subject); print result;if __name__=="__main__": fenge();
Both scripts can implement the string every two, separated by a space between the function, the first Python script no problem, the result of the second script output, each line below a blank line, but from the perspective of functional implementation, two code can be implemented, which is better for everyone to choose, recommend the first Oh.
PS: I python learning, we have other ways of implementation and problems, welcome message Discussion Oh ~
The python implementation separates a string of characters every two, with a space between them