Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中的文本文件中查找特定字符串_Python_String_Text_Find - Fatal编程技术网

如何在Python中的文本文件中查找特定字符串

如何在Python中的文本文件中查找特定字符串,python,string,text,find,Python,String,Text,Find,============== 这将为DATA.txt提供以下文本: while True: contents = pyperclip.paste() #paste from clipboard filepath = r"C:\Users\BOT\Desktop\DATA.txt" with open(filepath, 'w') as f: # 'w' means write mode and we get the file object as f

============== 这将为DATA.txt提供以下文本:

while True:
contents = pyperclip.paste() #paste from clipboard
filepath = r"C:\Users\BOT\Desktop\DATA.txt"
with open(filepath, 'w') as f: # 'w' means write mode and we get the file object as f
                        f.write(contents)
                        f.close() #write on DATA.txt, save
                with open(filepath, 'r') as f:(edited)
如何查找最后一个字符串?“出口短开” 这里的输出应该是2

440    Entry Short    Short    2018-04-01 17:37:00    6479.5    
    Exit Short    Close position order    2018-04-01 17:39:00    6477    17    42.5
441    Entry Long    Long    2018-04-01 17:39:00    6477    
    Exit Long    Close position order    2018-04-01 17:41:00    6513.5    51    1861.5
442    Entry Short    Short    2018-04-01 17:41:00    6513.5    
    Exit Short    Close position order    2018-04-01 17:43:00    6503    68    714
443    Entry Long    Long    2018-04-01 17:43:00    6503    
    Exit Long    Close position order    2018-04-01 17:44:51    6517    85    1190
444    Entry Short    Short    2018-04-01 17:45:06    6518.5    
    Exit Short    Open
445    Entry Short    Short    2018-04-01 18:45:06    6525.5    
    Exit Short    Open
不工作

可能是
内容。计数('Exit Short Open')
会起作用吗? 诸如此类:

filepath = r"C:\Users\BOT\Desktop\DATA.txt"
                with open(filepath, 'w') as f: # 'w' means write mode and we get the file object as f
                        f.write(contents)
                        f.close()
                with open(filepath, 'r') as f: 
                        f.read(contents)
                        if 'Exit Short    Open' in open.f.read():
                        print('Exit Short    Open')
                        f.close()

你能修复缩进使它成为一个可运行的程序吗?此外,发布完整错误,而不是“不工作”。这是
f.read(contents)
的一个例外吗?最后,考虑将此问题缩减到有问题的部分。。。。粘贴和写入文件对于这个bug来说不是必需的。答案很好,但我认为用块发布整个
。OP正在做一个无意义的
f.read(contents)
和一个不必要的
f.close()
结尾。这一切都需要清理。@tdelaney Done)仍然有一个bug。。。您读取文件两次(第二次得到一个空字符串)
while True:
    contents = pyperclip.paste() #paste from clipboard
    filepath = r"C:\Users\BOT\Desktop\DATA.txt"
    with open(filepath, 'w') as f: # 'w' means write mode and we get the file object as f
        f.write(contents)
        # f.close() no need to close the file, "with" operator does that automatically
    with open(filepath, 'r') as f:
        count_var = f.read().count('Exit Short    Open')
        print(count_var)