小编car*_*zer的帖子

如何使用Python在文本文件中找到字符串的第一个实例?

我试图搜索文本文件的第一个字符串实例并返回第一行中的所有其他内容,但我当前的程序找到并返回带有字符串的最后一行.

知道我需要做什么吗?

谢谢!

这就是我的代码:

#Open search file as read only
with open(fullpath, 'r') as searchfile:

     #Clear variable names
     partname = partsize = None

     #Search file for strings, trim lines and save as variables
     for line in searchfile:

         if "PART FILE NAME" in line:
             x = line
             partname = x[18:-1]

         if "PART SIZE" in line:
             y = line
             partsize = y[18:-1]
     #Open csv file, write variables, close csv file
     storefile = open("C:/Documents and Settings/Desktop/blue pega3.csv", 'a')
     storefile.write("%s,%s,%s\n" %(partname, partsize, fullpath))
     storefile.close() …
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
5440
查看次数

标签 统计

python ×1