简体   繁体   中英

How to read two arguments in text file?

I'm starting to program in python. I have a question about reading two files inside a text file my code.

arq = open("output.txt", "w")
count = 0
file_path = "/homescript/ASN.txt"
with open(file_path) as file:
    for line in file:
        line = line.rstrip()
        if 'AS' in line:
            count = count + 1
            print("set policy", line,"then accept",line, file=arq)
print ("\n Display return",count,"lines")

my text file contains:

AS0000
192.168.0.1/24
192.168.1.0/24

I need print ip address after "then accept"

You need to use the .readlines method to get the lines in the file, not just try to iterate from the entire file object. If you want a raw dump, use file.read() .

arq = open("output.txt", "w")
count = 0
file_path = "/homescript/ASN.txt"
with open(file_path) as file:
    for line in file.readlines():
        line = line.rstrip()
        if 'AS' in line:
            count = count + 1
            print("set policy", line, "then accept",line, file=arq)
print ("\n Display return", count, "lines")
arq.close()

Also don't forget to .close() the file.

arq = open("output.txt", "w")
count = 0
file = open("/homescript/ASN.txt", "r")
t = []
for line in file.readlines():
    line = line.strip()
    t.append(line)
for i in range(len(t)):
if 'AS' in t[i]:
    count = count + 1
    print("set policy", t[i], "then accept", t[i+1])
    print("set policy", t[i], "then accept", t[i+2])
print("\n Display return", count, "lines")

输出

An easy way

count = 0
file_path = "/homescript/ASN.txt"

with open(file_path) as file, open("output.txt", "w") as arq:  # you can open several files at once
    lines = file.readlines()  # read all lines into a list
    for i in range(len(lines)):
        line = lines[i].rstrip() # extract i-th line
        if 'AS' in line:
            count += 1
            ip = lines[i + 1].rstrip()  # ip is in the following line
            print("set policy", line, "then accept", ip, file=arq)
            
print ("\n Display return", count, "lines")

As you can see, the code above reads the whole file and iterates over a list. If a line contains 'AS', the next line is extracted as the IP address. This is not the most pythonic solution, but for you, as a beginner, should be good enough.


A more pythonic one

With the pairwise from python 3 docs you can do it even more easily:

from itertools import tee

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

count = 0
file_path = "/homescript/ASN.txt"

with open(file_path) as file, open("output.txt", "w") as arq:
    for line, next_line in pairwise(file):
        if 'AS' in line:
            count += 1
            ip = next_line.rstrip()
            print("set policy", line, "then accept", ip, file=arq)      
            
print ("\n Display return", count, "lines")

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