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
  Does python guarantees that file.write() works without file.close() ? kryptomatrix 7 184 2 hours ago
Last Post: kryptomatrix
  insert value to specific line in CSV file asheru93 0 37 2 hours ago
Last Post: asheru93
  How do you replace a word after a match from a list of words in each line of a file? vijju56 1 104 Oct-17-2019, 03:04 PM
Last Post: baquerik
  How do I read the HTML files in a directory and write the content into a CSV file? glittergirl 1 111 Sep-23-2019, 11:01 AM
Last Post: Larz60+
  Read each line, replace string and save into a new file igormonteiro 2 164 Sep-15-2019, 01:24 PM
Last Post: buran
  How do I write class objects to a file in binary mode? Exsul1 7 409 Sep-14-2019, 09:33 PM
Last Post: snippsat
  Finding a specific line in a file Vqlk 3 168 Sep-07-2019, 08:20 PM
Last Post: Axel_Erfurt
  how do i write a cross-platform relative file path? pseudo 2 127 Aug-23-2019, 05:07 PM
Last Post: ThomasL
  read text file and write into html with correct link jacklee26 4 240 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 314 Jul-22-2019, 11:50 PM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)