Python Self-study Note (v) Python text manipulation

Source: Internet
Author: User

One, Python's own method

R:read Read

W:write Write

A:append Tail Line Append

After the command line enters Python

>>>d = open (' A.txt ', ' W ') #在对应路径下打开a. txt text, if not, create one and assign to the variable D

>>>d.write (' good\n hi ') #写入

>>>d.close () #关闭文件

>>>d = open (' A.txt ', ' R ')

>>>print D.readline () #读一行

Good

>>>print D.readline () #接着读下一行

Hi

>>>d.seek (0) #将游标重置

Print D.read (#传入数字), number of rows

Good

Hi

Second, the module method

Import Linecache

>>>linecache.getline ("A.txt", 2) 2 is the number of rows

>>>linecache.getlines ("A.txt")--put each row in the list

Saves the module string's help document to a file

Import Sys

Import string

f = open (' Test.log ', ' W ')

Sys.stdout = f #将标准的输出流绑定在文档的写入方法里面

Help (String) #输出

F.colse ()

Python Self-study Note (v) Python text manipulation

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.