How to implement Python read-write TXT file json file _python

Source: Internet
Author: User
Tags readline string to file

First step, open the file, there are two functions to choose from: Open () and file ()

①. f = open (' file.txt ', ' W ')
...
File.close ()

②. f = file (' File.json ', ' R ')
...

File.close () #记得打开文件时最后不要忘记关闭!

Open () and file () are Python built-in functions that return a file object that has the same functionality and can be replaced arbitrarily. Use syntax for:

f = open (FileName, access_mode= ' R ', Buffering=-1)

The 1th parameter is the filename, the 2,3 parameter has a default value, and parameter 2 determines the way to read ' R '? Or is it written in the way ' W '? or other way to open the file.

The way to open it is:

r--read; w--write; a--append, write from EOF, that is, write at the end of a file

r+ w+ a+--are all opened in read-write mode

rb--binary read; wb--binary write; rb+ wb+ ab+--binary Read-write

Example:

fp = open (' C:\Users\MPC\Desktop\. txt ') # By default read to open

fp = open (' Test.txt ', ' W ') # Write way open

fp = open (' Data.json ', ' a ') ) #追加方式打开

The second step is to manipulate the file

The file can be manipulated when the handle to the file object is obtained, such as the FP in the example.

The built-in operations for file objects are: input, output, move within files, and miscellaneous operations

1. Input

Function: Read (), ReadLine (), ReadLines ()

Read the contents of a file into a string variable/list

Read (): reads the entire file into a string variable

Example:

fp = open (' C:\Users\MPC\Desktop\ description. txt ')

all_file = Fp.read ()

Read () has an optional size parameter, which defaults to-1, indicating that the file will be read to the end (EOF)

ReadLine (): reads a row in the open file and returns the entire row including the line terminator to the string variable

ReadLine () also has an optional parameter size, default-1, which means that the read to line terminator stops

ReadLines (): reads the entire file, returns a list of strings, each element in the list is a string representing a row

Example:

fp = open (' C:\Users\MPC\Desktop\ description. txt ')

lines = Fp.readlines () for line in

lines:

...

Fp.close ()

or the 2nd 3 line is abbreviated as: For lines in Fp.readlines ():

After python2.3, because of the introduction of iterators and file iterations (i.e., file objects becoming their own iterators),

The above example has a more efficient way of implementing it:

fp = open (' C:\Users\MPC\Desktop\

.

txt ') for line in FP: ... Fp.close ()

It is recommended to use this method!

2. Output

Functions: Write (), Writelines ()

Output a string/list to a file

Write (): output string to File

>>>f= open (' Test.txt ', ' W ')

>>>f.write (' helloworld! '

) >>>f.close ()

>>>f= open (' Test1.txt ', ' W ')

>>>f.write (' welcome\nto\n china! '

) >>>f.close ()

>>>f= open (' Test1.txt ', ' W ')

>>>f.write (' welcome\nto\n china! '

) >>>f.close ()

Writelines (): Writes a list of strings to a file, noting that the line terminator is not automatically added, and if necessary, you must manually add a line terminator at the end of each line.

What does that mean? Look at the following example:

>>>s= [' Hello ', ' Buddy ']

>>>f= open (' Test.txt ', ' W ')

>>>f.writelines (s)

>> >f.close ()

>>>s= [' Hello \ n ', ' Buddy ']

>>>f= open (' Test.txt ', ' W ')

>>> F.writelines (s)

>>>f.close ()

>>>f = open (R ' I:\python\test.txt ', ' W ')

>>> F.write (' line\n ')

>>>f.write (' Second line\n ')

>>>f.write (' third line\n '

) >>>f.close ()

>>>lines = List (open (R ' I:\python\test.txt '))

>>>lines

[' Firstline\n ', ' Second line\n ', ' third line\n ']

>>>first,second,third = open (R ' I:\python\test.txt ')

>>>first '

firstline\n '

>>>second

' secondline\n '

>>>third

' thirdline\n '

3. Moving within files

Function: Seek () tell ()

Seek (): Move the file read pointer to the established location

Tell (): Returns the location of the file read pointer

Three modes of Seek ():

(1) F.seek (p,0) Move when the file P byte, absolute position

(2) F.seek (p,1) moved to p bytes after the current position

(3) F.seek (p,2) moves to p bytes after the end of the relative article

The above is a small series for everyone to bring the Python read-write TXT file JSON file realization method of all content, I hope that we support cloud Habitat Community ~

Related Article

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.