Python Forum
unexpected output while parsing file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unexpected output while parsing file
#3
The problem is that you have more entries with "svc:1" than with "@ttml" but zip is hidding you this as it will stop when the shortest iterable is empty.

One solution can be to obtain directly the mapping as a dictionary:
import re

macdn = {}

with open('pppoe.txt','r') as f:
    dn = None
    for line in f:
        if '@ttml' in line:
            dn = re.findall(r'\d{10}',line)[0]

        if 'svc:1' in line and dn is not None:
            mac = re.findall(r'mac:[a-fA-F0-9]{2}[:][a-fA-AF0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}',line)
            
            if dn in macdn:
                macdn[dn].extend(mac)
            else:
                macdn[dn] = list(mac)
            dn = None

for dn in macdn:
    print('{} {}'.format(dn, macdn[dn]))
This allows for several macs with same dns. You can invert the logic easily to obtain a dictionary for one mac with several dns)

If you need to keep the 2 list format for something the same idea works: do not append to the lists until you have a valid dn:
import re

dnlist = []
maclist = []
 
with open('pppoe.txt','r') as f:
    dn = None
    for line in f:
        if '@ttml' in line:
            dn = re.findall(r'\d{10}',line)

        if 'svc:1' in line and dn is not None:
            mac = re.findall(r'mac:[a-fA-F0-9]{2}[:][a-fA-AF0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}',line)
            
            dnlist.append(dn)
            maclist.append(mac)
            dn = None

for dn, mac in zip(dnlist, maclist):
    print('{} {}'.format(dn, mac))
Finally if you want to record all the mac you can just add the '-' to the dn list:
import re

dnlist = []
maclist = []
 
with open('pppoe.txt','r') as f:
    for line in f:
        if line.strip() == '-':
            dnlist.append(['-'*10])
    
        if '@ttml' in line:
            dn = re.findall(r'\d{10}',line)
            dnlist.append(dn)

        if 'svc:1' in line:
            mac = re.findall(r'mac:[a-fA-F0-9]{2}[:][a-fA-AF0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}[:][a-fA-F0-9]{2}',line)
            maclist.append(mac)

for dn, mac in zip(dnlist, maclist):
    print('{} {}'.format(dn, mac))
Reply


Messages In This Thread
unexpected output while parsing file - by anna - Apr-26-2018, 07:22 AM
RE: unexpected output while parsing file - by anna - Apr-27-2018, 08:00 AM
RE: unexpected output while parsing file - by killerrex - Apr-28-2018, 10:32 AM
RE: unexpected output while parsing file - by anna - Apr-28-2018, 05:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  unexpected EOF while parsing dawid294 1 463 Jan-03-2024, 04:22 PM
Last Post: deanhystad
  Unexpected output Starter 2 553 Nov-22-2023, 12:08 AM
Last Post: Starter
  Unexpected Output - Python Dataframes: Filtering based on Overlapping Dates Xensor 5 807 Nov-15-2023, 06:54 PM
Last Post: deanhystad
  Unexpected output while using random.randint with def terickson2367 1 568 Oct-24-2023, 05:56 AM
Last Post: buran
  Unexpected output from df.loc when indexing by label idratherbecoding 6 1,293 Apr-19-2023, 12:11 AM
Last Post: deanhystad
Video doing data treatment on a file import-parsing a variable EmBeck87 15 3,055 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,168 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,766 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Parsing xml file deletes whitespaces. How to avoid it? Paqqno 0 1,072 Apr-01-2022, 10:20 PM
Last Post: Paqqno
  Parsing a syslog file ebolisa 11 4,295 Oct-10-2021, 05:15 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020