Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 write to txt file one line
#1
Hi,

I have a txt file that contains 100 ip addresses with one on each line.

Exp of txt file:

1.1.1.1
12.43.123.123
154.4.1.1
3.3.3.3

I need to read these ip address then write a string plus the ip address to a one line string.

Exp of output txt file with "ip from txt file" would be the ip from the txt file read.

"sourceIP = '1.1.1.1' or destinationIP = '1.1.1.1' or sourceIP = '12.43.123.123' or destinationIP = '12.43.123.123'"
Quote
#2
change "/tmp/mylist.txt" to what you need

result = []
inputstring = """1.1.1.1
12.43.123.123
154.4.1.1
3.3.3.3"""
 
for line in inputstring.splitlines():
    result.append("%s %s" % ("sourceIP =", "'{}'".format(line)))

output =  '\n'.join(result)
print(output)
 
with open("/tmp/mylist.txt", 'w') as outfile:
    outfile.write(output)
Output:
sourceIP = '1.1.1.1' sourceIP = '12.43.123.123' sourceIP = '154.4.1.1' sourceIP = '3.3.3.3'
Quote
#3
Hi thank you for your help.

The string I am looking for the output.txt file is:

'sourceIP = '1.1.1.1' or destinationIP = '1.1.1.1' or sourceIP = '12.43.123.123' or destinationIP = '12.43.123.123' and so on and so on.

This seems to be working for me.

Any suggestions on how to improve this code?

Thanks again!


filehandle = open("C:\Temp\S2S.txt", 'r')
ips = filehandle.read()
filehandle.close()

x = ""
for i in ips.split('\n'):
    x += "sourceIP = '" + i + "' or destinationIP = '" + i + "' " + "or "

writehandle = open("C:\Temp\output2.txt", 'w')
writehandle.write(x)
writehandle.close()

Quote
#4
Then you get "or" on the end of the line. you can remove it after splitlines

x = ""
for i in ips.splitlines():
    x += "%s %s %s %s %s" % ("sourceIP =", "'{}'".format(i), "or destinationIP =", "'{}'".format(i), "or")
k = x.rfind("or")
x = x[:k] + "" + x[k+2:]
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  read text file and write into html with correct link jacklee26 4 153 Aug-02-2019, 05:48 AM
Last Post: jacklee26
  Best way of taking a date prefix from a line and forming a file path from it? Skaperen 5 248 Jul-22-2019, 11:50 PM
Last Post: Skaperen
  Python start from a specific string line and write? searching1 1 153 Jun-27-2019, 02:28 PM
Last Post: perfringo
  is it possible to use python to read and write from a jsx file? nadavrock 3 265 Jun-08-2019, 02:59 AM
Last Post: heiner55
  Trying to write one line, writes 2 Buddhism 1 203 Jun-05-2019, 04:45 AM
Last Post: buran
  write image into string format into text file venkat18 2 302 Jun-01-2019, 06:46 AM
Last Post: venkat18
  Counting words in the last line of a file Drone4four 15 869 May-31-2019, 09:30 PM
Last Post: snippsat
  How to write to csv file SriMekala 1 135 May-14-2019, 01:58 PM
Last Post: buran
  Avoiding empty line in writing process csv file go127a 4 330 May-10-2019, 01:33 PM
Last Post: go127a
  how can I write my function output on CSV file go127a 13 469 May-05-2019, 01:55 PM
Last Post: go127a

Forum Jump:


Users browsing this thread: 1 Guest(s)