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
  Ignore first few letters of a line when reading file. ShakeyPakey 16 350 May-30-2020, 02:17 PM
Last Post: BitPythoner
  Write SQLite query result to file hjk6734 1 67 May-27-2020, 12:17 PM
Last Post: menator01
  Append JSON's and write to file faqsap 4 178 May-15-2020, 04:20 PM
Last Post: faqsap
  Pyinstaller create this error :“File ”multiprocessing\connection.py“, line 691 Formationgrowthhacking 2 155 Apr-30-2020, 10:26 AM
Last Post: buran
  Delete all contents of a file from the fifth line? PythonNPC 1 186 Apr-18-2020, 09:16 AM
Last Post: buran
  Read a CSV file only from the 4th line/row asaforen7 8 420 Apr-16-2020, 01:49 AM
Last Post: Larz60+
  Print/write to file function tpolim008 4 217 Apr-01-2020, 07:59 PM
Last Post: tpolim008
  Write object to file medatib531 7 232 Apr-01-2020, 06:25 PM
Last Post: BrendanD
  Replace XML tag and write to the same file krish216 4 278 Mar-26-2020, 07:39 AM
Last Post: snippsat
  can we write command output to new csv file using Panda package? PythonBeginner_2020 3 330 Mar-13-2020, 12:38 PM
Last Post: ndc85430

Forum Jump:


Users browsing this thread: 1 Guest(s)