简体   繁体   中英

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.
  • 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.

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",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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