将结果集转换为字符串并将其放置在列表中 [英] Convert Resultset to string and place in list

查看:79
本文介绍了将结果集转换为字符串并将其放置在列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将列表中的值转换为普通字符串,例如

I'm trying to convert the values in my list to normal strings such as

listy = [['value1','value2','value3'],['value1','value2','value3'],[

我初始化了一个空列表listy = [],并使用find_all('a')找到了所有<a>并将其作为输出

I initialized an empty list listy = [] and found all the <a> using find_all('a') and produced this as output

listy = [[... <a>value1</a>, <a>value2</a>, <a>value3</a>, ...],[...<a>value4</a>, <a>value5</a>, <a>value6</a>, ...],[]]

我尝试使用item.find_all('a').string,但是却收到此错误.

I tried using item.find_all('a').string however I was given this error.

AttributeError: 'ResultSet' object has no attribute 'string'

意味着ResultSet不能转换为字符串.

Meaning the ResultSet cannot be converted to string.

然后我搜索如何将ResultSet转换为String

I then search how to convert ResultSet to String

我找到了unicode.join(u'\n', map(unicode,listy))

但是,当我尝试此操作时,出现此错误

However when I tried this i get this error

AttributeError: 'unicode' object has no attribute 'append'

我很可能会在附加列表以使我的原始列表出现错误时出现此错误

I most likely get this error when I appended the list to get my original listy

如何将find_all('a')转换为string类型并将其附加到列表中?

How could i convert the find_all('a') to string types and append them into a list?

我的XML文件:

<Z>
  <Y>
    <x>
      <a> value1 </a>
      <a> value2 </a>
      <a> value3 </a>
    </x>
  </Y>
  <Y>
    <x>
      <a> value4 </a>
      <a> value5 </a>
      <a> value6 </a>
    </x>
  </Y>
  <Y>
  </Y>
</Z>

我正在尝试创建以下格式的列表

I'm trying to create a list in the following format

listy = [['value1','value2','value3'],['value4','value5','value6'],[]]

推荐答案

您需要获取每个个人链接的字符串,而不是整个结果集的字符串.

You need to get the string of each individual link, not of the whole result set.

循环遍历集合并获取.string 每个元素:

Loop over the set and fetch .string per element:

[link.string for link in item.find_all('a')]

这篇关于将结果集转换为字符串并将其放置在列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆