Python_ Text Manipulation

Source: Internet
Author: User

1 #write content to a text file2s='Hello world\n text file Read method \ n Text file write method \ n'3F=open ('Sample1.txt','A +')#Open File4F.write (s)#Write file Contents5F.close ()#Close File6 7With open ('Sample1.txt','A +') as F:8 F.write (s)9 Ten #the context management statement with also supports the following usage: OneWith open ('Test.txt','R') as Src,open ('Test_new.txt','W') as DST: A Dst.write (Src.read ()) -  -  forLineinchOpen'Test.txt'): the     Print(line) -  - #Read text file contents -fp = open ('Test.txt') + #content within the Test.txt g4a6d4g6a5gaojdfoiayufiajgldjuaidfjldakjgalgm LADJGLFA98EPOFAJIIDFJLADJFAJF - Print(Fp.read (4))#reads the first 4 characters from the current position + #G4A6 A Print(Fp.read (18))#English subtitles are treated like Chinese characters at #D4g6a5gaojdfoiayuf - #How to read a text file - Print(Fp.read ())#reads everything that follows from the current location - #IAJGLDJUAIDFJLDAKJGALGM LADJGLFA98EPOFAJIIDFJLADJFAJF - #How to write a text file -Fp.close ()#Close File Object
1 " "2 reads and displays all lines of a text file, and the file object is an iterative3 " "4With open ('Sample.txt') as FP:5     Print(Type (FP))6      whileTrue:7Line=Fp.readline ()8         if  notLine :9              BreakTen         Print(line) One  A  -With open ('Sample.txt') as FP: -      forLineinchFp:#a file object can be iterated the         Print(line) -  -With open ('Sample.txt') as FP: -lines = Fp.readlines ()#Do not recommend this when working with large files +     Print("'. Join (lines)) -  + " " A moves the file pointer. Assume that the contents of the file sampple.txt are originally ' Hello world\n text file Read method \ n Text file Write method ' at " " -FP =open ('Sample.txt','r+') - Print(Fp.tell ())#returns the current position of the file pointer - #0 - Print(Fp.read (20))#Read 20 characters - #Tiger 2017 years old: in #belong to the tiger - #201 toFp.seek (13)#re-ding W.Q. file pointer position + Print(Fp.read (5)) - #years old: theFp.seek (100) *Fp.write ('Test Move') $Fp.flush ()#Write buffer contents to disk FilePanax Notoginseng fp.seek (0) - Print(Fp.read ()) theFp.close ()

Note: The text file that appears is created by itself

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.