有一个由字符串组成的列表,我想将字符串里面的数据拿出来组成新的列表。于是写了下列代码
a = [ "ddr(ip='::', port=8009)", "ddr(ip='::rrr', port=8090)" ]
print(a)
P_conn_l_2 = []
for item in a:
P_conn_l_2.append(
[ item.ip, item.port ]
)
print(P_conn_l_2)
运行后报错
```bash
["ddr(ip='::', port=8009)", "ddr(ip='::rrr', port=8090)"]
Traceback (most recent call last):
File "tttt.py", line 6, in
[ item.ip, item.port ]
AttributeError: 'str' object has no attribute 'ip'
import re
a = [ "ddr(ip='::', port=8009)", "ddr(ip='::rrr', port=8090)" ]
print(a)
P_conn_l_2 = []
for item in a:
P_conn_l_2.append(
[re.search("ip='(.*?)'", item).group(1), re.search('port=(\d+)', item).group(1) ]
)
print(P_conn_l_2)
你a数组下面是两个字符串,你当成数组用了肯定不对啊,你也输出不出来