Python tuples, file operations

Source: Internet
Author: User

Beginners have just begun to learn python, if there is a wrong write or write a bad place, please give us a lot of guidance!
Python tuple additions
A = (up to)
b = (3,4)
A + b
Tuples using multiplication
* 4 #在这里边, tuples are not counted as numbers, but output 4 times
To the alphabet type of the tuple shot
t = (' BB, ', ' dd ', ' AA ', ' CC ')
TM = List (t)
Tm.sort () #然后输出tm
t = Tuple (TM)
Use the For method to calculate
t = (1,2,3,4,5)
L = [x + = x in T]
Replace tuple
t = (1,[2,3],4)
T[1][0] = ' Spa ' #t元组中第二个数值之后紧挨着的数值
Python file operations
Common file Operations
Output = open (R ' d:\a.py ', ' W ') creates an export file (W refers to write)
input = open (' Date ', ' R ') to create a file (R refers to read-write)
input = open (' date ') wants to be the same as the previous line (R is the default value)
Input.read () reads the entire file into a single string
Input.read (n) reads n bytes after, to a string
Input.readline () reads the first row, reads the next line the first time.
Alist = Input.readlines () reads the entire file into a list of strings
Output.write (AS) writes a byte string to a file
Output.writelines (alist) writes all the strings in the list to the file
Output.close () manual shutdown (the file will be closed for you when the file collection is completed)
Output.flush () brushes the output buffer to the hard disk, but does not close the file
Anyfile.seek (n) Modify the file location to offset N for the next operation
For lines in open (' Data '): The use of the line file iterator to a row of reads
Open (' F.txt ', encoding= ' latin-1 ') python3.0unicode text file (str string)
Open (' F.bin ', ' RB ') python3.0 binary byte file (bytes string)

Instance Application
MyFile = open (' MyFile.txt ', ' W ') #创建一个myfile. txt file and open for writing
Myfile.write (' hello,world\n ')
Myfile.write (' Good bye ' \ n) #\n a career change
Myfile.close () #关闭文件 then open the local directory to see if the contents of the file are the same
Read file
MyFile = open (' myfile.txt ') #打开文件, default is read-only
Myfile.readline () #读取第一行
Myfile.readline () #读取下一行
Read the entire file into a single string
Open (' MyFile.txt '). Read () #把所以文件一次性读取完, \ n indicates the next line
Use Print to read
Print (Open (' myfile.txt '). Read ()) #这样处理的结果比较清晰, interlaced
To read the file line by row in a for way
For line in open (' MyFile.txt '):
Print (line,end= ")
Open a file as a binary method
data = open (' MyFile.txt ', ' RB '). Read () #这样的话效果不太明显, you can create a text to write the numbers open to see
Data[4:8]
DATA[0]
Bin (data[0]) #二进制的方式显示一个文件

File storage
X, y, z = 43, 44, 45
s = ' spam '
D = {' A ': 1, ' B ': 2}
L = [n/A]
f = open (' Data.txt ', ' W ')
F.write (s + ' \ n ') #直接将s插入然后转行
F.write ('%s,%s,%s\n '% (x, y, z))
F.write (str (l) + ' $ ' str (d) + ' \ n ') #str输出l + str output D
Then read to see the results
A = open (' Data.txt '). Read ()
Print (a)
Remove the extra rows
f = open (' Data.txt ')
line = F.readline ()
Line.rstrip () #移除掉用了转行的 \ n
Create 2 binary files
D = {' A ': 1, ' B ': 2}
f = open (' datafile.pk ', ' WB ')
Import Pickle
Pickle.dump (d, F)
F.close ()
Want to read the time, because the conversion to binary, but also with Pickle read back
f = open (' datafile.pk ', ' RB ')
A = Pickle.load (f) #在这不知道是什么原因, sometimes this is defined as an error.
Pickle.load (f) #如果报错的话, just to read
Directly open binary files
Open (' datafile.pk ', ' RB '). Read () #显示的就是一堆二进制数字 instead of the inserted value

Using a struct module to package and parse binary files
Start by creating
f = open (' Data.bin ', ' WB ')
Import struct
data = Struct.pack (' >i4sh ', 7, ' spam ', 8)
F.write (data)
F.close ()
Then read
f = open (' Data.bin ', ' RB ')
data = F.read ()
Values = Struct.unpack (' i4sh ', data) #然后输出values

Beginners have just begun to learn python, if there is a wrong write or write a bad place, please give us a lot of guidance!

This article is from the "Bucket Month" blog, please be sure to keep this source http://douyue.blog.51cto.com/10174393/1652464

Python tuples, file operations

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.