简体   繁体   English

打印变量并在循环中在文件中附加一行

[英]Printing a variable and appending it with a line from a file in a loop

I've researched this already but I can't find an exact answer. 我已经对此进行了研究,但找不到确切答案。 I've found answers for appending when there's 2 lists involved but this is different so here goes. 当涉及到2个列表时,我找到了追加的答案,但这是不同的,所以这里是。

I'm creating a really basic fuzzer but I'm having issues appending the directory names to the end of the address. 我正在创建一个非常基本的模糊器,但是在将目录名称附加到地址末尾时遇到了问题。 Here's what I have so far. 到目前为止,这就是我所拥有的。

The expected output is as follows; 预期输出如下:

www.website.com/1
www.website.com/2
www.website.com/3
www.website.com/4

etc. But I'm getting something completely different. 等等。但是我得到了完全不同的东西。 Here's the first piece of code I tested. 这是我测试的第一段代码。

>>> host = "www.website.com/"
>>> path = [line.strip() for line in open("C:/Users/Public/Documents/tester.txt", 'r')]
>>> print str(host) + str(path)

which returns the following 返回以下内容

www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

The second attempt was this; 第二次尝试是这样的;

>>> host = "www.website.com/"
>>> path = [line.strip() for line in open("C:/Users/Public/Documents/tester.txt", 'r')]
>>> for line in path:
print str(host) + str(path)

Which returned this; 哪个退回了;

www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
www.website.com/['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

I can see exactly what's happening and why it's happening but I can't figure out how to arrive at the expected output. 我可以确切地了解正在发生的事情以及发生的原因,但是我无法弄清楚如何得出预期的输出。 I'll also need to filter out the special characters which I didn't think 'print' would pick up. 我还需要过滤掉一些我认为“打印”不会使用的特殊字符。 Maybe there's different rules for print when it's reading something as a list. 当它以列表形式读取内容时,也许有不同的打印规则。

I've thought of stupid complex methods such as counting the lines in the file and then throwing it into a while loop using that count but I'm sure there's something I can use or something I've done wrong. 我想到了愚蠢的复杂方法,例如对文件中的行进行计数,然后使用该计数将其放入while循环中,但是我确定我可以使用某些东西或做错了什么。 My Python knowledge isn't fantastic. 我的Python知识不是很出色。

Can anybody help with this? 有人可以帮忙吗?

You were nearly there , when using the for loop , concatenate the elements from the list (which in your case is in variable 'line') , not the complete list again . 您几乎在那儿,使用for循环时,将列表中的元素(在您的情况下位于变量'line'中)连接起来,而不是完整列表。

Code - 代码-

for line in path:
    print str(host) + str(line)

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

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