Python-数组不输出整个值而只是字符

时间:2019-04-06 23:00:27

标签: python regex

我希望下面的代码现在可以在数组中输出整个存储的值,当我指定下一个字符时,它只是放置第一个字符

所需:

['6.1', '6.2', '6.3', '6.4', '6.5']
6.1
6.2
6.3
6.4
6.5

实际:

['6.1', '6.2', '6.3', '6.4', '6.5']
6
6
6
6
6

问题:

print (idPercentageDetailsArray[0])

这是问题。我想我正在请求数组索引0,但我却正在获取每个字符串的位置0。不清楚为什么。任何帮助将不胜感激。

代码:

    import re

    processDetails = """
        Percent Done: 6.1%

        Percent Done: 6.2%

        Percent Done: 6.3%

        Percent Done: 6.4%

      Percent Done: 6.5%"""

    idPercentageDetails = re.findall(r'Percent Done:\s+([0-9]+[.,][0-9]+|[0-9]+)', processDetails, flags=re.MULTILINE)

    print (idPercentageDetails)

    for idPercentageDetailsArray in idPercentageDetails:
        print (idPercentageDetailsArray[0])

1 个答案:

答案 0 :(得分:0)

只需更改行:

print (idPercentageDetailsArray[0])

print (idPercentageDetailsArray)

由于[0]意味着您获得 字符串 的第一个字符:idPercentageDetailsArray


因此,您可能希望将变量名更改为如下所示,以使代码更有意义:

import re

process_details = """
    Percent Done: 6.1%

    Percent Done: 6.2%

    Percent Done: 6.3%

    Percent Done: 6.4%

  Percent Done: 6.5%"""

id_percentage_details = re.findall(r'Percent Done:\s+([0-9]+[.,][0-9]+|[0-9]+)', process_details, flags=re.MULTILINE)

print(id_percentage_details)

for id_percentage_value in id_percentage_details:
    print(id_percentage_value)

输出:

['6.1','6.2', '6.3', '6.4', '6.5']
6.1
6.2
6.3
6.4
6.5