[The Python] file read and write

python file object provides three "read" approach: the Read (), readline () and readlines () . Each method can accept a variable to limit the amount of data read each time.

  • read () each read the entire file , the file is typically used content into a string variable. If the file is larger than available memory, to be safe, you can repeatedly call read(size)the method, each read up to size bytes of content.
  • readlines () once read the entire file , like .read () the same. .readlines () automatically analyzes the contents of the file into a list of lines , this list can be processed by the structure in ... Python's for ....
  • readline () a time to read one line , usually much slower than readlines (). Only when there is not enough memory to read the entire file once, you should use the readline ().

Note : These three methods is the end of each line '\ n' read came in, it does not default to '\ n' removed, we need to manually removed.


python file object provides two "write" method: the Write ()  and writelines () .

  • write () method and a read (), readline () method corresponds to the string is written to the file.
  • the writelines () method and the readlines () method corresponding to, but also for the list of operations. It receives a list of strings as arguments, to write them to a file, the newline is not automatically added, therefore, requires an explicit join line breaks .

About open () mode parameter :

'R': Read

'W': write

'A': Append

'R +' == r + w (readable and writable, if the file exists on the error (IOError))

'W +' == w + r (read and write, the file is created if present)

'A +' == a + r (writable can be added, if there is a file created)

Correspondence, if it is a binary file, you can add a thousand million b:

'RB' 'we' 'baby' 'RB +' 'I +' bitter '+'


example:

    There are two files, each with many rows ip address, find the same ip address two files:

# coding:utf-8
import bisect

with open('test1.txt', 'r') as f1:# 'with'can close file automaticlly
    list1 = f1.readlines()
for i in range(0, len(list1)):
    list1[i] = list1[i].strip('\n')
with open('test2.txt', 'r') as f2:
    list2 = f2.readlines()
for i in range(0, len(list2)):
    list2[i] = list2[i].strip('\n')

list2.sort()
length_2 = len(list2)
same_data = []
for i in list1:
    pos = bisect.bisect_left(list2, i) #二分法
    if pos < len(list2) and list2[pos] == i:
        same_data.append(i)
same_data = list(set(same_data))#利用set的唯一性去除重复项
print(same_data)

bisect  for ordering  a sorted array module and insertion operations.

A method comprising:

     bisect    bisect_left     bisect_right     insort    insort_left     insort_right

     Which bisect is called bisect_right, insort is called insort_right

 

bisect_left(a, x, lo=0, hi=None)

- an object thereof is to find the value will be inserted position and back, without insertion. If x  is present in a position x in the left return

import bisect

li = [1, 23, 45, 12, 23, 42, 54, 123, 14, 52, 3]
li.sort()
print(li)
print(bisect.bisect_left(li, 3))


result:
[1, 3, 12, 14, 23, 23, 42, 45, 52, 54, 123]
1

insort_left(a, x, lo=0, hi=None)

- inserting the element x in a list, the sort sorted and held. If x is already in a, insert it into the left and right of x.

import bisect

li = [1, 23, 45, 12, 23, 42, 54, 123, 14, 52, 3]
li.sort()
print(li)
bisect.insort_left(li, 3.0)
print(li)

result:

[1, 3, 12, 14, 23, 23, 42, 45, 52, 54, 123]
[1, 3.0, 3, 12, 14, 23, 23, 42, 45, 52, 54, 123]

 

Published 89 original articles · won praise 17 · views 40000 +

Guess you like

Origin blog.csdn.net/lbt_dvshare/article/details/88597292