String 使用索引python访问列表值

String 使用索引python访问列表值,string,list,split,concatenation,python-3.8,String,List,Split,Concatenation,Python 3.8,所以,我遇到了一些非常有趣的事情。我用.splitlines()拆分了一个字符串。 当我打印该列表时,它工作正常,并以这种形式输出值:['Freitag 24.11','08:00']。 但是,如果我试图通过索引访问该列表的任何值,例如list[0],它应该给我该列表的第一个值。在本例中为“Freitag 24.11” splittedParameters = tag.text.splitlines() print(splittedParameters[0]) 如前所述,如果我不使用索引[0]

所以,我遇到了一些非常有趣的事情。我用.splitlines()拆分了一个字符串。 当我打印该列表时,它工作正常,并以这种形式输出值:['Freitag 24.11','08:00']。 但是,如果我试图通过索引访问该列表的任何值,例如list[0],它应该给我该列表的第一个值。在本例中为“Freitag 24.11”

splittedParameters = tag.text.splitlines()
print(splittedParameters[0])
如前所述,如果我不使用索引[0],它就会正常工作并输出整个列表。但在索引的形式中,它说:“indexer-ror:列表索引超出范围”

整个代码:

    from requests_html import HTMLSession

    startDate = None
    endDate = None
    summary = None
    date = ''
    splittedDate = ''

    url = 'ANY_URL'

    session = HTMLSession()
    r = session.get(url)
    r.html.render()

    aTags = r.html.find("a.ui-link")

    for tag in aTags:
        splittedParameters = tag.text.splitlines()
        print(splittedParameters[0])
不返回任何内容,如给定的no值

aTags中没有文本

print(aTags)

>>> [<Element 'a' class=('logo', 'ui-link') href='index.php'>]
我什么也没印出来

我发现了错误。 问题是在for循环的第一次运行中,“splittedParameters”列表是空的

    if len(splittedParameters) > 0:
        print(splittedParameters[0])

这将起作用。

是否
splittedParameters
=['Freitag 24.11','08:00']您能给我们看看
tag.text.splitlines()
在真实代码中是什么样子的吗请解释一下tag.text包含的内容您应该在某个地方输入错误,我无法重现您的问题。我建议你单独做一个测试,比如:tag.text包含一个字符串,看起来像:“Montag 23.11\n09:10”你看到我上传的图片了吗?为什么它不起作用?你可以将代码复制到问题中,或者向我展示真实代码中的
splittedParameters
是什么样子,比如在粘贴中,或者在打印中
print(type(splittedParameters))
并向我展示你得到的信息,但是在for循环中,我迭代了ATAG,每个标记中都有属性文本,这是一个字符串,我也可以打印出来
for tag in aTags:
    print(tag.text)
    splittedParameters = tag.text.splitlines()
    if len(splittedParameters) > 0:
        print(splittedParameters[0])