4

我有一个看起来像这样的列表:

wsadmin>print jvmLines
['', 'Stats name=jvmRuntimeModule, type=jvmRuntimeModule#', '{', 'name=HeapSize, ID=1, description=The total memory (in KBytes) in the Java virtual machine run time., unit=KILOBYTE, type=BoundedRangeStatistic, lowWaterMark=1048576, highWaterMark=1048576, current=1048576, integral=0.0, lowerBound=1048576, upperBound=2097152', '', 'name=FreeMemory, ID=2, description=The free memory (in KBytes) in the Java virtual machine run time., unit=KILOBYTE, type=CountStatistic, count=176789', '', 'name=UsedMemory, ID=3, description=The amount of used memory (in KBytes) in the Java virtual machine run time., unit=KILOBYTE, type=CountStatistic, count=871786', '', 'name=UpTime, ID=4, description=The amount of time (in seconds) that the Java virtual machine has been running., unit=SECOND, type=CountStatistic, count=3809648', '', 'name=ProcessCpuUsage, ID=5, description=The CPU Usage (in percent) of the Java virtual machine., unit=N/A, type=CountStatistic, count=1', '}']

为什么会这样:

for line in jvmLines:
    if "name=" in line:
        print line

导致这个?

TypeError: string member test needs char left operand

如果我尝试 lambda 函数或 filter(),我也会得到相同的结果

4

2 回答 2

9

试试find()方法

for line in jvmLines:
    if line.find('name=') >= 0:
        print line
于 2013-10-03T14:55:46.283 回答
0

我收到此消息是因为我返回的是单个值而不是数组。IE

# Causes Error
# serverList = {'default': 'server001'}
if s in serverList[‘default’]

# Works Fine
# serverList = {'default': ('server001', 'server002')} 
if s in serverList[‘default’]

当返回的值是我预期的列表时,它可以正常工作,但如果它是单个值,则“in”会引发错误。

于 2016-01-28T22:22:00.750 回答