【笨方法】”字符串截取“
基本字符串1=”abc123“
基本字符串2=”345aaa“
例如:目标字符串为:Today
is
a
good
day
aaa123目标字符串345aaa
那么:
获得基本字符串1的长度:len1=len(基本字符串1)
获得基本字符串2的长度:len2=len(基本字符串2)
---------------------------------------------------------------------
以len1长度开始截取目标字符串,以上面的例子为例,截取出来的应该为:
Today_
oday_i
day_is
ay_is_
y_is_a
等...........................
..............
当然这些都是一个循环就可以搞定,然后在这个循环里,对每次接触的字符串进行比对,如果找到与目标字符串形同的,则记下”索引“
开始进行下一步处理:截取本句剩下的部分,找到”基本字符串2“,然后记下其开始”索引“,那么两个”索引“之间的东东就是你想要的那个”目标字符串“,之后你想用它干什么都行...........
【超简单的方法】
会”正则表达式“吗?会的话,直接用正则吧,几句就出来了..........
1 利用python的readlines()函数:[python] view plain copy
<strong><span style="font-size:24px"> </span><span style="font-size:14px">fobj = open(r'Ori.Data.txt','r')
for line in fobj.readlines()[1000:]
fobj.close()</span></strong>
2 利用 linecache
[python] view plain copy
<strong><span style="font-size:14px"> import linecache
print(linecache.getline(r'D:\z.txt',10))</span></strong>
3 读取10行到13行中的内容
[python] view plain copy
<span style="font-size:14px"><strong> lnum = 0
with open('pit.txt', 'r') as fd:
for line in fd:
lnum += 1
if (lnum >= 10) &&(lnum <= 13):
print line
fd.close()</strong></span>
4 求文本的行数
[python] view plain copy
<span style="font-size:14px"><strong> fobj = open('Ori_Data.txt','r')
row_len = len(fobj.readlines()) </strong></span>
[python] view plain copy
<span style="font-size:14px"><strong>
</strong></span>
[python] view plain copy
<span style="font-size:14px"><strong>fobj = open(filepath,'r')
data = fobj.read()
fobj.close()
text_len = data.count('\n')<span style="font-family: Arial, Helvetica, sans-serif"></span></strong></span>
python 3.3 代码
import sysreader = open('scores.txt')
line = reader.readline()#读取第一行数据
scores = []#放 分数值 的数值
stander = 0#及格人数
while line != '' and line != None:#循环读取数据行
tempScore = line.split(' ')[1].replace('\n','')#将姓名和成绩分开,并取分数
scores.append(tempScore)#将得到的分数添加到数组中
if float(tempScore) >= 60:#记录大于60分的成绩
stander += 1
line = reader.readline()
reader.close()
print(scores)
print(stander)