简体   繁体   English

将引号附加到python文件中的链接

[英]append quotation marks to links in a file in python

I need to append few quatations to the below data我需要在下面的数据中附加几个公式

10.10.10.10:8000
10.10.10.10:8001

I used the command below我使用了下面的命令

file_lines = ['"http":','"http://"'+,.join([' '])for x in f.readlines(),+',"']

the output required is所需的输出是

“http”: “http://10.10.10.10:8000”,

“https”: “http://10.10.10.10:8001”, 

:) :)

I don't know if this is what you need, but I coded something real quick.我不知道这是否是您需要的,但我很快就编写了一些代码。

But keep in mind:但请记住:

  • I have no idea how you check whether it's HTTP or HTTPs.我不知道你如何检查它是 HTTP 还是 HTTPS。
  • Your "join" part doesn't make sense, since it will loop over the array of data and make a string like " http://10.10.10.10:8000 "," http://10.10.10.11:8000 ",... instead of interpreting the array's items as a new line everytime.您的“加入”部分没有意义,因为它会遍历数据数组并生成一个字符串,如http://10.10.10.10:8000”、“http://10.10.10.11:8000、。 ..而不是每次都将数组的项解释为一个新行。

So I hope this helps:所以我希望这会有所帮助:

lineList = [line.rstrip('\n') for line in open("path_to_data.txt")]
outArr = []
for line in lineList:
    outArr.append('"http": ' + '"http://'+ line + '",')

for item in outArr:
    print(item) 

I tested it with this data:我用以下数据对其进行了测试:

10.10.10.10:8000
10.10.10.11:8000
10.10.10.12:8000
10.10.10.13:8000
10.10.10.14:8000
10.10.10.15:8000
10.10.10.16:8000
10.10.10.17:8000

And this is my output:这是我的输出:

"http": "http://10.10.10.10:8000",
"http": "http://10.10.10.11:8000",
"http": "http://10.10.10.12:8000",
"http": "http://10.10.10.13:8000",
"http": "http://10.10.10.14:8000",
"http": "http://10.10.10.15:8000",
"http": "http://10.10.10.16:8000",
"http": "http://10.10.10.17:8000",

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

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