Python Forum
Morning Removing last character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Morning Removing last character
#4
(Sep-06-2019, 03:17 PM)DeaD_EyE Wrote: Use str.join

My output:
Output:
deadeye@nexus ~ $ python2.7 parse_ips.py Without piping to program, you have to use --input-file deadeye@nexus ~ $ python2.7 parse_ips.py --input-file usage: parse_ips.py [-h] [--input-file INPUT_FILE] parse_ips.py: error: argument --input-file: expected one argument deadeye@nexus ~ $ python2.7 parse_ips.py --input-file hosts.txt ^10\.10\.10\.10$|^10\.10\.10\.11$|^10\.10\.10\.12$|^10\.10\.10\.13$|^10\.10\.10\.14$ deadeye@nexus ~ $ cat hosts.txt | python2.7 parse_ips.py ^10\.10\.10\.10$|^10\.10\.10\.11$|^10\.10\.10\.12$|^10\.10\.10\.13$|^10\.10\.10\.14$
Yep thanks .. I understand that he "." means any char. The App Im pasting into recognizes an ip just so long its wrapped in ^$.

Thanks.

Code:
#!/usr/bin/env python2.7
from __future__ import print_function
import sys
import argparse


def ip2regex(text):
    ips = []
    for row in text.splitlines():
        try:
            ip, hostname = row.split()
        except ValueError:
            # skip errors
            continue
        ip = '^' + ip.replace('.', r'\.') + '$'
        ips.append(ip)
    return '|'.join(ips)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--input-file', required=False, help='Input file to generate regex output.')
    args = parser.parse_args()
    if args.input_file is None and not sys.stdin.isatty():
        print(ip2regex(sys.stdin.read()))
    elif args.input_file and sys.stdin.isatty():
        with open(args.input_file) as fd:
            print(ip2regex(fd.read()))
    else:
        print('Without piping to program, you have to use --input-file', file=sys.stderr)
Line 15-17 preparing the IP address. By the way, a dot is a metachar in regex. The dot stands for any kind of char.

If you use the dot without escaping it, the regex ^10.10.10.10$ will be also match: 10510710310

PS: split is the opposite of join.

Ive seen this construct in some example code .. but not in any instruction .... probably because Im just starting out.

What is it called and where can I learn about it ..
ips = [ip_addr for line in f for ip_addr, *_ in line.split()]
Reply


Messages In This Thread
Morning Removing last character - by sumncguy - Sep-06-2019, 01:53 PM
RE: Morning Removing last character - by DeaD_EyE - Sep-06-2019, 03:17 PM
RE: Morning Removing last character - by sumncguy - Sep-06-2019, 04:59 PM
RE: Morning Removing last character - by buran - Sep-06-2019, 03:27 PM
RE: Morning Removing last character - by buran - Sep-06-2019, 05:16 PM
RE: Morning Removing last character - by sumncguy - Sep-06-2019, 05:36 PM
RE: Morning Removing last character - by DeaD_EyE - Sep-06-2019, 11:59 PM
RE: Morning Removing last character - by sumncguy - Sep-16-2019, 03:40 PM
RE: Morning Removing last character - by buran - Sep-16-2019, 03:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] unexpected character after line continuation character paul18fr 4 7,660 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 4,332 Jul-13-2020, 07:05 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 3,744 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Replace changing string including uppercase character with lowercase character silfer 11 8,955 Mar-25-2019, 12:54 PM
Last Post: silfer
  SyntaxError: unexpected character after line continuation character Saka 2 20,733 Sep-26-2017, 09:34 AM
Last Post: Saka

Forum Jump:

User Panel Messages

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