-3

我需要解析和清理自定义 API 的一些结果。下面是存储为变量的示例输出。

(DeviceConnectionInformation){
     DeviceName = "device1"
     ClientName = "ourclient"
     RegionName = "west"
     PrimaryIP = "1.1.1.1"
     OsVersionName = "Microsoft Windows 7 Enterprise"
     AccessMethod = None
     AccessIP = None
     Port = None
  },
  (DeviceConnectionInformation){
     DeviceName = "device2"
     ClientName = "ourclient"
     RegionName = None
     PrimaryIP = "1.1.1.2"
     OsVersionName = "Microsoft Windows 7 Enterprise"
     AccessMethod = None
     AccessIP = None
     Port = None

我需要删除所有可能出现的“(DeviceConnectionInformation){”和“}”,并保留以下格式:

DeviceName = "device1"
ClientName = "ourclient"
RegionName = "west"
PrimaryIP = "1.1.1.1"
OsVersionName = "Microsoft Windows 7 Enterprise"
AccessMethod = None
AccessIP = None
Port = None

DeviceName = "device2"
ClientName = "ourclient"
RegionName = None
PrimaryIP = "1.1.1.2"
OsVersionName = "Microsoft Windows 7 Enterprise"
AccessMethod = None
AccessIP = None
Port = None
4

2 回答 2

1

假设您的数据是字符串形式,您可以通过将其替换为空来去除文字字符串:

val = val.replace("(DeviceConnectionInformation){", "")
val = val.replace("},", "")

但是,我对此持谨慎态度,尤其是第二种情况,因为它不注意上下文。例如,如果},您的数据出现在带引号的字符串中,它仍然会被删除。

于 2013-04-16T19:42:18.220 回答
0

How about

for x in ("DevicConnectionInformation{\n", "},", "     "):
    data = data.replace(x, "")

of you get into trouble if, e.g., }, appears in the values.

于 2013-04-16T19:44:17.600 回答