Python Text Processing (2)

Source: Internet
Author: User

Access the sub-string (obtain the data of some fields in the record) solution: slice, however, you can only obtain one field [python] theline = 'I Love python' afield = theline [] print afield result Love. If you need to consider the length of the field, use struct. unpack [python] #-*-coding: gb2312-*-import struct baseformat = '1s 6x 6s '# first, baseformat defines how to group strings. x indicates skipping, s indicates taking out # So the code above indicates taking away 1 character, skipping 6 characters, and taking away 6 Characters theline = 'I Love Python So much' numremain = len (theline) -struct. calcsize (baseformat) # Of course, the length of the theline string must be greater than 6 + 6 + 1, therefore, we need to calculate the remaining number of strings format = '% s % s' % (baseformat, numremain) # regrouping. Now we include the remaining strings l, s1, s2 = struct. unpack (format, theline) # use format and theline to perform the unpack operation on print l, s1, and s2. If you want to obtain data in a group of 5 bytes, use the slicing method with list derivation [python] theline = 'I Love Python So much' fivers = [theline [k: k + 5] for k in xrange (0, len (theline), 5)] print fivers result ['I Lov', 'e pyt', 'Hon s', 'O muc ', 'H'] replacing the substring in a string solution: [python] def expand (format, d, marker = '"', safe = False): if safe: def lookup (w): return d. get (w, w. join (marker * 2) else: def lookup (w): return d [w] parts = format. split (marker) parts [1: 2] = map (lookup, parts [1: 2]) return ''. join (parts) if _ name _ = '_ main _': print expand ('just "a" test', {'A ': 'one'}) after Python2.4, you can use string. template Class to complete this task [python] #-*-coding: gb2312-*-import string # generate a Template from the string, where the identifier is $ marked new_style = string. template ('this is $ thing ') # input a dictionary parameter print new_style.substitute ({'thing': 5}) print new_style.substitute ({'thing ': 'test'}) # You can also pass the keyword parameter print new_style.substitute (thing = 5) print new_style.substitute (thing = 'test ') result this is 5 this is test this is 5 this is test in Python2.3, you can use the following method: [python] #-*-coding: gb2312-*-old_style = 'this is % (thing) s' # The identifier is placed in brackets. Use the % operator. Put the dictionary print old_style % {'the' on the right side ': 5} print old_style % {'thing ': 'test'} This method can also be used in Python2.4, but it is less concise than the new method.

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.