python操作文字 python操作文字

2021-10-11 19:26:45 字數 1037 閱讀 8132

>>> d.write('hi.\nsecond hi.')

>>> d.close()

>>> d=open('a.txt','r')

>>> d.readline()

'hi.\n'

>>> d.readline() #一次讀一行,指標會改變

'second hi.'

>>> d.readline() #一次讀一行,指標會改變

>>> d.seek(0) #文字的指標重置為0

>>> d.read(100) #表示一次讀100個位元組

'hi.\nsecond hi.'

>>> a = open('tmp.txt','w') #檔案不存在會自動建立

>>> a.write(1) #只能寫字串或者是字元流

traceback (most recent call last):

file "", line 1, in ?

typeerror: argument 1 must be string or read-only character buffer, not int

>>> a.close()

>>> b=open("tmp.txt",'r')

>>> b.read(500)

>>> b.seek(0)

>>> b.readline()

標準庫的介紹 linecache>>> import linecache

>>> print linecache.getline("tmp.txt",1)

>>> print linecache.getline("tmp.txt",2)

hhloo

>>> print linecache.getline("tmp.txt",3)

ni hoa

>>> lines=linecache.getlines("tmp.txt")

>>> lines

>>> help(linecache) 檢視幫助

# cat /usr/lib64/python2.7/linecache.py 檢視原始碼

Python文字操作 檔案指標

1.seek 跳到指定的檔案位置 coding utf 8 如果沒有這個檔案,新建乙個檔案,檔名為 myallfile.txt fileopen open d pythontext myallfile.txt r if name main file.seek 16,0 0檔案調到從開頭開始第十五個字...

c 中操作文字

建立文字 string path streamwriter sw file.createtext path sw.write hello sw.writeline hello sw.close 讀取文字 string str string address filestream fs string p...

oracle操作文字日期

首先要對文字進行字串轉換 to char substr p.product attr json,instr p.product attr json,valuedate 13,14 然後對字串進行轉換 to date time,yyyy mm dd substr var1,start,length 對...