Python 将字符串转换为列表,然后是一些

Python converting string to list and then some

基本上我要做的是接受输入(见下文)并将格式转换为以下输出(见下文)。输出是字典列表。我一直在玩 .split() 和 .strip() 但我仍然无法将 IP 地址与房间号分开。 (请参阅下面的代码)

输入:

"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)

输出:

[{'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)

我的代码:

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

这是我得到的输出: {'ip': '144.38.196.157";119', 'mac': '00:23:AE:90:FB:5B', 'name': 'tellurium', 'room': None}

关闭但没有雪茄,试图分开 119

尝试:

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

Split Strings with Multiple Delimiters?

这会将分号替换为 space,然后拆分。所提供的 link 将提供更通用的解决方案来拆分多个定界符。

下面的列表理解从 line 中删除双引号,然后拆分分号,然后从行中的每个字段中删除前导和尾随白色 space。然后它使用元组分配将字段提取到命名变量。

#! /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

输出

"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'}

我应该提一下,来自您 for line in file: 的每一行都将以换行符结尾;我的代码将其与列表理解中带有 s.strip() 的其他白色 space 一起删除。未能从文本文件输入行中删除换行符可能会导致神秘和/或烦人的行为...

使用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

使用正则表达式提取:

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

输出:

[{'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'}]

要删除前面有 ;119,只需 split 分号:

line.split(';')

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

在您的代码中:

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

将值存储到变量后

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))