0 Basic Learning Python text processing

Source: Internet
Author: User

Source| little Elephanttext| Jia

This article will lead the small partners together, using Python for text processing, first look at the text to be processed, the file name is "Data.txt", the contents of the file is three lines of English-text, and two blank lines,

The function we want to achieve is from the three lines of text extracted from the Chinese and its corresponding English, this function, coupled with the crawler, you can make their own English dictionary, is not very cool, and very real

With, and so what, hurriedly on the code

this To take care of the zero-based children's shoes, so to start from the simplest building block, first run the code to see the effect


Seemingly just print out the text of the content, you say yes, it is so simple, we pursue the effect is everything beginning simple, middle simple, the end simple:)

Although there are only two lines of code, but for the real 0 basis of children's shoes, I am afraid I can only understand the meaning of print:)

So to explain verbatim, the first is open ("Data.txt"), which is to tell Python, open a file called "data.txt"; What do you do when you open it? Through the for line reading the contents of the file, some children's shoes may first contact This grammar, feel not understand, this is OK, in fact, not do not understand, but the new knowledge needs to adapt, more knock several times the code, every day knock over, not one weeks, will

Feel unusually cordial, do not believe can try, spend 3 minutes a day, a total of 21 minutes a Monday, really effective oh.

And then it's print, and Line says that every time it reads a row, including a blank line, print, which prints out what's being read.

The 3 knowledge points in front of the master, children's shoes are already an introduction! And then the V2, or the effect first.

Some children's shoes can not help shouting: garbled! Pro, MO panic, these square brackets are not garbled, is the grammar. In Python, a square bracket [] represents a list, yes, List is a sell, oh, not sell, is used:)

A list can be empty, for example, two are empty list, can also contain multiple elements, such as the other 3 list, each list contains 3 elements, in this case each element is a string, with a pair of single quotes to denote the start and end of the string, Have children's shoes will ask double quotes, this can be:

In the same list, the elements and elements are separated by commas.

The results of the output we understand, the next step is to see the source

The addition of a line to the V2 version, New_line, is the two operations (which can also be understood as two processing of lines), which is the list of elements that we saw earlier.

So what do you do with line?

To illustrate the processing of line , we define a set of variables, namely an empty string (variable named emptystring), a character (Stra), a character suffix a space (Strawithtrailingwhitespace), the string that is obtained after the Rstrip () operation of the string with the suffix space (strarstriped). is not a bit like tongue twisters, please do not skip, if temporarily skipped, please come back carefully read again ha, actually y is to experience the function of Rstrip ():

Let's look at it one by one, the first is the empty string

The so-called empty string, which is the string does not contain any characters, so the length (that is, Len (emptystring)) for the 0,print This empty string of course nothing to see, in order to let everyone see the effect, in print when added a "."

Then there is a string with one character (the variable named stra), so the length (that is Len (Stra)) is 1, then the print string plus one "."

With the front cushion, the knowledge to knock on the blackboard to come! The variable we want to define here is a character suffix (Trailing) with a space

(whitespace) variable name fully reflects this:), so the length is 2, please note that when print, "a" and "." There is a clear space between, and can be selected with the mouse OH


The last variable name in this section is strarstriped, that is, the new variable that gets the strawithtrailingwhitespace variable to go to the suffix space, so the length is changed back to 1, note that when print is "a" and "." The space between the Rstrip was dropped.

To make it easy for children's shoes to understand, use two lists to store the original text without Rstrip, and the processed text, as shown in the control effect

then the split () method, which is split, divides a line of text into small pieces, and the default delimiter (delimiter) is a space (whitespace), and the empty string is deleted from the result. The effect is as follows

Finally, the final version, Liste storage English, LISTC store the corresponding Chinese

-end-

Copyright NOTICE: This article for the small Elephant original article, reprint please contact backstage.


0 Basic Learning Python text processing

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.