0

Basically what I am trying to do is take an input (see below) and convert the format to the following output (see below). The output being a list of dictionaries. I have been playing with .split() and .strip() but I still having issues in separating the IP address with the room number. (see my code below)

input:

"bromine ";" 00:23:AE:90:FA:C6 ";" 144.38.198.130";151 #(this is just one line in the file, there are several lines with this exact format)

output:

[{'ip': '144.38.198.130', 'mac': '00:23:AE:90:FA:C6', 'name': 'bromine', 'room': '151'}] #(again this would be just one of the lines)

My code:

import sys

my_list = []
file = sys.stdin
for line in file:
   # d = {}
    line = line.strip('"')
    line = line.split()

    name = line[0]
    macAddress = line[2]
    ipAddress = line[4]
    #roomNum = [?]

    d={'ip': ipAddress, 'mac': macAddress, 'name': name, 'room': None}
    my_list.append(d)
    #print line

print d

This is the output I'm getting: {'ip': '144.38.196.157";119', 'mac': '00:23:AE:90:FB:5B', 'name': 'tellurium', 'room': None}

Close but no cigar, trying to separate the 119

lqhcpsgbl
  • 3,694
  • 3
  • 21
  • 30
Wrenzoe
  • 41
  • 6

5 Answers5

2

The list comprehension below removes double quotes from line, then splits on semi-colons, then strips leading & trailing white space from each field in the line. Then it extracts the fields to named variables using tuple assignment.

#! /usr/bin/env python

line = '"bromine ";" 00:23:AE:90:FA:C6 ";" 144.38.198.130";151'
print line

line = [s.strip() for s in line.replace('"', '').split(';')]
print line

name, macAddress, ipAddress, roomNum = line
d = {'ip': ipAddress, 'mac': macAddress, 'name': name, 'room': roomNum}

print d

output

"bromine ";" 00:23:AE:90:FA:C6 ";" 144.38.198.130";151
['bromine', '00:23:AE:90:FA:C6', '144.38.198.130', '151']
{'ip': '144.38.198.130', 'mac': '00:23:AE:90:FA:C6', 'name': 'bromine', 'room': '151'}

I should mention that each line coming from your for line in file: will end in newline characters; my code removes that along with other white space with the s.strip() in the list comprehension. Failure to remove newlines from text file input lines can lead to mysterious &/or annoying behaviour...

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
1

Try:

line.replace(';',' ').split()

Split Strings with Multiple Delimiters?

This replaces semicolon with space, then splits. The link provided will give a more general solution to splitting on multiple delimiters.

Community
  • 1
  • 1
Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
0

Extracting using replace/strip:

import sys

my_list = []
f = sys.stdin

for line in f:
    line = line.split('";')
    result = dict(
                  zip(["name", "mac", "ip", "room"],
                  [field.replace('"', "").strip() for field in line]))
    my_list.append(result)

print my_list

Extracting using regex:

import sys
import re

my_list = []
f = sys.stdin

pattern = r'"(\w*)\W*([\w:]*)\W*([\w\.]*)";(\w*)'
for line in f:
    result = dict(
        zip(["name", "mac", "ip", "room"],
            re.match(pattern, line).groups()))
    my_list.append(result)

print my_list

Output:

[{'ip': '144.38.198.130', 'mac': '00:23:AE:90:FA:C6', 'name': 'bromine', 'room': '151'},
{'ip': '144.38.196.157', 'mac': '00:23:AE:90:FB:5B', 'name': 'tellurium', 'room': '119'}]
f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
  • 3
    Regex is not the solution to all problems, and for an introductory question like this, it's far too complicated. Not to mention that that pattern is significantly more difficult to read than the corresponding Python code... – sapi Jan 24 '15 at 06:09
0

To remove the 119, which is preceded by a ;, just split by the semi-colon:

line.split(';')

Return a list of the words in the string, using sep as the delimiter string.

In your code:

import sys

my_list = []
file = sys.stdin
for line in file:
   # d = {}
    line = line.strip('"')
    line = line.split()[0]
    name = line.split(';')[0]

    macAddress = line[2]
    ipAddress = line[4]
    #roomNum = [?]

    d={'ip': ipAddress, 'mac': macAddress, 'name': name, 'room': None}
    my_list.append(d)
    #print line

print d
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

After storing the valued in a variable

import re
input = '"a";"b";"c";"d"'
keys = ['x','y','z','w']
inputlist = input.split(';')
for x in range(0, len(inputlist)):
    inputlist[x] = re.sub(r'"','',inputlist [x])
output = dict(zip(keys,inputlist))
lucifer
  • 110
  • 8