Python/Jython:如果string中的substring总是导致TypeError:string member test需要char left operand

See*_*eer 3 python string jython list filter

我有一个如下所示的列表:

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', '}']
Run Code Online (Sandbox Code Playgroud)

为什么这样:

for line in jvmLines:
    if "name=" in line:
        print line
Run Code Online (Sandbox Code Playgroud)

导致这个?

TypeError: string member test needs char left operand
Run Code Online (Sandbox Code Playgroud)

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

小智 8

试试这个find()方法

for line in jvmLines:
    if line.find('name=') >= 0:
        print line
Run Code Online (Sandbox Code Playgroud)