Python字符串笔记
1.字符串 str = 'abcd'
在引用str 希望得到abcd时
kkk = str + 'cdef'
ttt = open("/home/suyuhan123/" + str + '.txt')
引用字符时直接写str 不要写$str "str" 等。 str 直接就表示了 'abcd'
2.代码中有中文时 写
# coding=utf-8
3. 字符串分割,一定要判断长度
file = open("fileDir")
for line in file:
tt = line.split(',')
---此时一定要判断长度
length = len(tt)
if length < 0 :
continue
4.Python 不读第一行
from itertools import islice
for line in islice(oldfile,1,None):
5.创建新文件时 只要 open('新文件名','wb') 即可
模式:
w 只写文件,如果没有文件:创建,如果文件存在:清空
w+ 可读写文件,如果文件不存在:创建,如果有:清空
r 只读文件,该文件必须存在。
r+ 可读写的文件,该文件必须存在。
a 附加方式打开文件,只写
a+ 附加方式打开文件,可读写
上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,表示打开的文件为二进制文件,而非纯文字文件。
不过在Linux系统会忽略该字符。
1.字符串 str = 'abcd'
在引用str 希望得到abcd时
kkk = str + 'cdef'
ttt = open("/home/suyuhan123/" + str + '.txt')
引用字符时直接写str 不要写$str "str" 等。 str 直接就表示了 'abcd'
2.代码中有中文时 写
# coding=utf-8
3. 字符串分割,一定要判断长度
file = open("fileDir")
for line in file:
tt = line.split(',')
---此时一定要判断长度
length = len(tt)
if length < 0 :
continue
4.Python 不读第一行
from itertools import islice
for line in islice(oldfile,1,None):
5.创建新文件时 只要 open('新文件名','wb') 即可
模式:
w 只写文件,如果没有文件:创建,如果文件存在:清空
w+ 可读写文件,如果文件不存在:创建,如果有:清空
r 只读文件,该文件必须存在。
r+ 可读写的文件,该文件必须存在。
a 附加方式打开文件,只写
a+ 附加方式打开文件,可读写
上述的形态字符串都可以再加一个b字符,如rb、w+b或ab+等组合,表示打开的文件为二进制文件,而非纯文字文件。
不过在Linux系统会忽略该字符。