Python implements three methods to process one character each time, and python three methods

Source: Internet
Author: User

Python implements three methods to process one character each time, and python three methods

This example describes three methods for python to process a single character each time. Share it with you for your reference.

The specific method is as follows:

a_string = "abccdea"  print 'the first' for c in a_string:   print ord(c)+1     print "the second"   result = [ord(c)+1 for c in a_string] print result  print "the thrid"  def do_something(c):   return ord(c)+1  result = map(do_something ,a_string) print result 

The output is as follows:

the first 98 99 100 100 101 102 98 the second [98, 99, 100, 100, 101, 102, 98] the thrid [98, 99, 100, 100, 101, 102, 98] 

I hope this article will help you with Python programming.


Python handles string processing issues in files:

The answer for the upstairs is changed to the first line.
F1 = open('1.txt ')
No other problems

However, there is a simpler way:

F1 = open('1.txt ')

Linen = 1

For line in reader:
If not line. find ('44. 2% ') =-1:
Print linen
Linen + = 1

F1.close ()

If it is helpful to you, please remember to adopt a satisfactory answer. Thank you! Wish you a happy life!
 
How does python retrieve a string from a file?

If the file is not large
Import re
F = open ('xxxx. xx', 'R'). read ()
FList = re. split (R' \ s + ', f)

OK. All of them are included in the fList list.

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.