如何使python代码读取.txt文件中的特定文本行?

时间:2018-09-15 17:25:12

标签: python

所以我一直在摆弄python和英雄联盟。我发现您可以在游戏中做笔记。所以我考虑过让python代码从游戏中的注释中读取一行文本,例如“ Lux no flash”,但它似乎根本无法读取,只有当我手动执行时它才能工作使用完全相同的代码。这是我的代码:

import os
import time
def main():
    os.chdir('C:\\Riot Games\\League of Legends\\RADS\\solutions\\lol_game_client_sln\\releases\\0.0.1.237')
    f=open("MyNotes.txt", "r")
    if f.mode == 'r':
        lines=f.readlines()
        text = lines[4]
        time.sleep(0.1)
        if text == 'Lux no flash':
           print('Done')
        else:
            print('Something went wrong')
    f.close()
if __name__== "__main__":
 main()

输出是“出了点问题”,但是当我手动执行时,它说“完成”。我觉得python无法阅读联赛代码。也许你们知道该怎么做...这是我尝试访问的.txt文件:

##################################################
2018-09-13_18-57-33_
##################################################
Lux no flash

2 个答案:

答案 0 :(得分:2)

我只是在假设的基础上拍摄文件:

(defn filmglumci [listaglumaca]
(layout/main-base
[:div.table100
 [:table
  [:thead
   [:tr.table100-head
    [:th.column1 "Ime i prezime"]
    [:th.column2 "Godina snimanja"]
    [:th.column3 "Drzava"]
    [:th.column4 "Glumac"]
    [:th.column5]
    ]]
  [:tbody
   (for [item (map
                (fn [glumac]
                  [:tr
                   [:td.column1 (:imeprezime glumac)]
                   [:td.column2 (:godinasnimanja glumac)]
                   [:td.column3 (:nazivdrzave glumac)]
                   [:td.column4 (:username glumac)]
                   [:td.column5 [:a {:class "btn btn-dark" :id "edit" :href 
        (str "/film/glumci/" (:glumacid glumac) "/dodaj")} "Dodaj"] " "]])
                listaglumaca)] item)]]]))

因此,只需在文件中寻找# cat MyNotes.txt there is Lux no flash in line there is Something went wrong There is nothing lux no flash this is all test 一词,我们就可以按照以下步骤进行操作。但是它区分大小写。

使用'Lux no flash'方法读取文件始终是最佳实践。

with open()

输出结果将是:

import os
import time
def main():
       with open("MyNotes.txt") as f:
        for line in f.readlines():
            if 'Lux no flash' in line:
                print('Done')
            else:
                print('Something went wrong')
if __name__== "__main__":
    main()

即使尝试使用Done Something went wrong Something went wrong Something went wrong ,它也可以按我的代码正常工作。

lux.txt

结果出局是:

import os
import time
def main():
    with open("lux.txt") as f:
        for line in f.readlines():
            #line = line.strip()  # use can use the strip() or strip("\n")
            #line = line.strip("\n") # if you see white spaces in the file
            if 'Lux no flash' in line:
                print('Done')
            else:
                pass
if __name__== "__main__":
    main()

答案 1 :(得分:2)

使用lux.txt

##################################################
2018-09-13_18-57-33_
##################################################
Lux no flash

代码:

content = []
with open('lux.txt', 'r') as f:
    for line in f:
        content.append(line.strip('\n'))

    for i in content:
        if 'Lux no flash' == i:
            print("Done")
        else:
            pass

更好 @pygo

with open('lux.txt', 'r') as f:
    content = f.read()
    if 'Lux no flash' in content:
        print("Done")
    else:
        print("No else, this works :)") 

输出:

(xenial)vash@localhost:~/python/stack_overflow$ python3.7 lux.py
Done