简体   繁体   English

如何在没有 \n 的情况下从 txt 中获取特定行(Python)

[英]How to get a specific line from a txt without \n (Python)

So basically my code grabs a line from my txt and does its job with selenium.所以基本上我的代码从我的 txt 中抓取一行,并使用 selenium 完成它的工作。 the problem is after every check, my program should give me the status after the selenium check and the line that has been checked.问题是在每次检查之后,我的程序应该在 selenium 检查和已检查的行之后给我状态。 the problem is both of the functions are in different def's.问题是这两个功能都在不同的def中。 the selenium part and the check status part. selenium 部分和检查状态部分。

so i could do x = f.readline() and call it after the check is done which will work with no issues tho because the x is needed in the 1. def and it's changing every time, i can't change his position.所以我可以做 x = f.readline() 并在检查完成后调用它,这将没有问题,因为 1.def 中需要 x 并且它每次都在变化,我无法更改他的 position。 so i tried to do y = f.readline() in the status def, which should also work but because i call readline again, both of the variables change.所以我尝试在状态 def 中执行 y = f.readline(),这也应该有效,但是因为我再次调用 readline,这两个变量都发生了变化。 i need it to check and report line by line.我需要它逐行检查和报告。 Also readline() Copies \n which is causing a lot of trouble还有 readline() 复制 \n 这会造成很多麻烦

Let me make it a little bit more clear.让我说得更清楚一点。 here is the more simple code这是更简单的代码

f = open('list.txt', 'r')

def part1():
    x = f.readline()
    xxx = x[0:][:16]
    xxxx = x[17:][:2]
    driver.get('https://website.com')
    elem = driver.find_element_by_name("example")
    elem.send_keys(xxx);
    elem = driver.find_element_by_name("example2")
    elem.send_keys(xxxx);
    ......

def checkpart():
    part1()
    (some sleep code)
    if driver.find_elements_by_xpath("//*[contains(text(), 'checkprocess')]"):
        print('[-] Bad: ', x)
    elif driver.find_elements_by_xpath("//*[contains(text(), 'checkprocess2')]"):
        print('[+] Good: ', x)
    else:
        print('[+] Smthelse: ', x)

(some input questions...)

i = 1

register() //other function thats not needed here//

while i <= input:
        checkpart()
        time.sleep(1)
        i += 1

Its a bit complicated and i couldnt found an easier way of readline().它有点复杂,我找不到更简单的 readline() 方法。 tho it doesnt allow me to display x in the checkpart and it also copies \n虽然它不允许我在检查部分中显示 x 并且它还复制 \n

I need to display the current checked line (which is x) without changing it and without copying \n in the end of it.我需要显示当前选中的行(即 x)而不更改它并且不复制 \n 在它的末尾。

As i said its a bit complicated for me i've made a lot of research tho didnt come up with anything that will help.正如我所说,这对我来说有点复杂,我做了很多研究,但没有想出任何有用的东西。 i also issue the same in php.我也在 php 中发布了相同的内容。

Instead of using the filehandle itself (variable f), you could read all text into memory and work from there.您可以将所有文本读入 memory 并从那里开始工作,而不是使用文件句柄本身(变量 f)。 You won't have to worry about the positioning of the cursor in your file stream.您不必担心 cursor 在文件 stream 中的定位。

f = open('list.txt', 'r')
content = f.readlines()
f.close()

for line in content:
    part1(line)
    checkpart(line)

Now you can work with the variable content without a headache现在您可以轻松处理变量内容

You could do the filereading in checkpart() and simply pass x onto part1(x).您可以在 checkpart() 中进行文件读取,然后简单地将 x 传递给 part1(x)。 And to avoid the trailing newline, you could use x.rstrip()为了避免尾随换行符,您可以使用 x.rstrip()

f = open('list.txt', 'r')

def part1(x):
    xxx = x[0:][:16]
    xxxx = x[17:][:2]
    driver.get('https://website.com')
    elem = driver.find_element_by_name("example")
    elem.send_keys(xxx);
    elem = driver.find_element_by_name("example2")
    elem.send_keys(xxxx);
    ......

def checkpart():
    x = f.readline().rstrip()
    part1(x)
    (some sleep code)
    if driver.find_elements_by_xpath("//*[contains(text(), 'checkprocess')]"):
        print('[-] Bad: ', x)
    elif driver.find_elements_by_xpath("//*[contains(text(), 'checkprocess2')]"):
        print('[+] Good: ', x)
    else:
        print('[+] Smthelse: ', x)

(some input questions...)

i = 1

register() //other function thats not needed here//

while i <= input:
        checkpart()
        time.sleep(1)
        i += 1

Have you tried reading the file?您是否尝试过读取文件? You can then use split to create or list for example:然后,您可以使用 split 创建或列出例如:

f = open("list.txt", "r")

f_read = f.read()

f_list = f_read.split("\n")

May print(x, end = "") will be sufficient?可能print(x, end = "")就足够了吗?

Could you give additional requirements?你能给出额外的要求吗?

Alternatively you can strip just last character with x[:-1], but as I observe, you are already aware of this possibility.或者,您可以使用 x[:-1] 删除最后一个字符,但正如我所观察到的,您已经意识到这种可能性。

--- EDIT --- BTW, as mentioned above, it probably is better to read whole file (or portion) to list, and then iterate list. --- 编辑 --- 顺便说一句,如上所述,最好将整个文件(或部分)读取到列表中,然后迭代列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM