The python implementation separates a string of characters every two, with a space between them

Source: Internet
Author: User
Tags python script

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.