str = str.split('=')AttributeError:'list'对象没有属性'split'

时间:2019-06-28 06:24:51

标签: python xpath split

我必须将xpath中的数据拆分为两个字段

str = response.xpath('.//td//div[@class="cit_maoin"]()')
str=split('=')

print('published)

4 个答案:

答案 0 :(得分:0)

line 3

中尝试一下
str=str[0].split('at')

您的列表中只有一项,因此索引将为0,而不是1。索引从列表中的0开始。 希望能有所帮助!

答案 1 :(得分:0)

str是Python中的关键字。 让my_str = 'published = 6/11/2019 at 8:02 AM'

my_str = my_str.split('=')[1]

date_str = my_str.split('at')[0]
time_str = my_str.split('at')[1]

print(date_str) #6/11/2019
print(time_str) #8:02 AM

有关输入列表,请使用for循环。

答案 2 :(得分:0)

尝试

str = ['published = 6/11/2019 at 8:02 AM']
published_date, time = str[0][11:].split('at')[0],str[0][11:].split('at')[1] 

按如下所示检查并假设您的mystr

my_str = [['\r\n ', '3/28/2019 at 8:52 PM.\r\n This list includes 36 
        eligible players.\r\n ', '\r\n \xa0\r\n '],['\r\n ', '3/28/2019 at 8:52 PM.\r\n This list includes 36 eligible players.\r\n']]



date = []
time = []
for x in my_str:
    index_ = x[1].find('PM')
    string_ =  x[1][0:index_ + 2]
    date.append(string_.split('at')[0])
    time.append(string_.split('at')[1])

#op
print(date)
['3/28/2019 ', '3/28/2019 ']

print(time)

[' 8:52 PM', ' 8:52 PM']

答案 3 :(得分:0)

尽量不要使用str,因为它是Python中的内置关键字。您没有指定所需的输出,但是在注释中,我认为您需要对代码进行如下更改:

    data = ["published = 6/11/2019 at 8:02 AM"]
    results = []
    for s in data:
      t = s.split("=")
      z = t[1].split("at")
      tmp = f'published date = {z[0]} published time = {z[1]} '
      results.append(tmp)

   print(results) # ['published date =  6/11/2019  published time =  8:02 AM '] 

使用此代码,您将获得所需输出的列表。希望对您有帮助