#1
for line in matched_lines[0:2]:
#2
print (line)
#3
file = open("pythonsnmp.txt", "w")
#4
x = str(matched_lines[0:2])
#5
file.write(x)
#6
file.write('\n')
#7
file.close()
In the #2 the result is:
(146, '[interfaces.ifTable.ifEntry.ifDescr.8],(STRING),HPE Ethernet 1Gb 2-port 361T Adapter #2')
(162, '[interfaces.ifTable.ifEntry.ifDescr.24],(STRING),HPE Ethernet 1Gb 2-port 361T Adapter')
But in the txt file the result is:
[(146, '[interfaces.ifTable.ifEntry.ifDescr.8],(STRING),HPE Ethernet 1Gb 2-port 361T Adapter #2'), (162, '[interfaces.ifTable.ifEntry.ifDescr.24],(STRING),HPE Ethernet 1Gb 2-port 361T Adapter')]>
If I want the txt file same as #2, how can I code it?
You can unpack the slice as arguments for a call to the print
function, which has a keyword argument file
that specifies the file stream to write to:
with open("pythonsnmp.txt", "w") as file:
print(*matched_lines[0:2], file=file, sep='\n')
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.