鱼C论坛

 找回密码
 立即注册
查看: 2613|回复: 10

Python 循环查找 求助

[复制链接]
发表于 2017-2-13 14:44:02 | 显示全部楼层 |阅读模式
20鱼币
想实现一个功能,比如查找一个字符串‘Helloworld’ 第一次查找到的时候 输出1,第二次查找到字符串‘Helloworld’的时候输出2,第三次查找到的时候输出3。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-2-13 14:58:08 | 显示全部楼层
只求个数的话:str.count()
每次有其他操作的话字符传按下标截取:str=str[str.find("Helloworld")+1:]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-2-13 15:02:40 | 显示全部楼层
ycgzs98789 发表于 2017-2-13 14:58
只求个数的话:str.count()
每次有其他操作的话字符传按下标截取:str=str[str.find("Helloworld")+1:]

可以 详细点吗? 我想实现的功能就是  现在有三个get请求 ,get请求(可以理解为一个字符串)是一样的。 第一次搜索到这个get请求 写入文件1  第二次搜索到这个get 写入文件2  第三次 搜索到这个get 写入 文件3  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-2-13 15:41:50 | 显示全部楼层
本帖最后由 ycgzs98789 于 2017-2-13 15:46 编辑

if str.find("Helloworld"):
   文件1写入
   str=str[str.find("Helloworld")+1:]
   if str.find("Helloworld"):
           文件2写入
           str=str[str.find("Helloworld")+1:]
           if str.find("Helloworld"):
                  文件3写入

---------------------------------------------------------
if str.count("Helloworld") >= 3:
    文件3写入
if str.count("Helloworld") >= 2:
    文件2写入
if str.count("Helloworld") >= 1:
    文件1写入
这个刚才弄错了

--------------------------------------------
直接敲的,有错勿怪。
其实应该写个循环的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-2-13 15:44:31 | 显示全部楼层
ycgzs98789 发表于 2017-2-13 15:41
if str.find("Helloworld"):
   文件1写入
   str=str[str.find("Helloworld")+1:]

非常 感谢你的帮助。我自己也尝试写了个,还在尝试。待会试试你的~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-2-14 23:00:54 | 显示全部楼层
本帖最后由 5387100 于 2017-2-14 23:02 编辑
  1. import re

  2. def writeToFile(fname,content):
  3.     with file(fname,'a') as f:
  4.         f.write( content + '\n\r' )
  5.         

  6. def main():
  7.     search = r'he\D\Do'
  8.     string1 = 'aa hesloo bb heblo cc heelo dd healo'
  9.     m = re.findall(search, string1)
  10.     for matched in m:
  11.         writeToFile('d:\\result.txt', matched)

  12. if __name__ == '__main__':
  13.     main()
复制代码



在d:\result.txt会生成这样:

heslo
heblo
heelo
healo
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-2-15 22:07:30 | 显示全部楼层
不知道是不是这么个意思
  1. a=[[],[],[]]
  2. yuan='HelloworldHelloworldHelloworldHelloworldHelloworldHelloworldHelloworld'
  3. mubiao='Helloworld'
  4. ly=len(yuan)
  5. lm=len(mubiao)
  6. cishu=0
  7. for i in range(ly-lm+1):
  8.     if yuan[i:i+lm]==mubiao:
  9.         a[cishu%3].append(mubiao)
  10.         cishu+=1
  11. print(a)
复制代码

结果分别放到a[0],a[1],a[2]里,如果要保存到文件里,一样的道理
  1. RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python35-32/test.py
  2. [['Helloworld', 'Helloworld', 'Helloworld'], ['Helloworld', 'Helloworld'], ['Helloworld', 'Helloworld']]
  3. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-2-17 06:11:38 | 显示全部楼层
用正则, 匹配到时写入文件, 之后再用剩下的再匹配, 直到没有匹配到为止
perl code:
while(1){
  if($str =~ /hello/){写入}
  $str = $';
}
#$'为没有进行匹配的那一部分字符串

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-2-17 06:12:32 | 显示全部楼层
喜欢散步 发表于 2017-2-17 06:11
用正则, 匹配到时写入文件, 之后再用剩下的再匹配, 直到没有匹配到为止
perl code:
while(1){

记得要跳出while(1)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-2-17 07:59:00 | 显示全部楼层
学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-2-17 16:29:45 | 显示全部楼层
厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 03:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表