我用过这个
print(listing_jobs)
导致了这个
['Senior Cloud Specialist\nFull-time · Singapore · 5 - 10 Years\n12 days ago', 'Cloud Native Developer\nFull-time · Hyderabad · 2 - 5 Years\n13 days ago']
如何转换\n
为逗号?
当我使用
listing_jobs.strip().split('\n')
它显示错误
AttributeError: 'list' object has no attribute 'strip'
如果我理解你的问题很好,你想列表的元件分开listing_jobs
的\n
?如果是这样,您可以使用列表理解如下:
d = ['Senior Cloud Specialist\nFull-time · Singapore · 5 - 10 Years\n12 days ago',
'Cloud Native Developer\nFull-time · Hyderabad · 2 - 5 Years\n13 days ago']
d = [x.strip().split("\n") for x in d]
这将给出以下列表列表:
[['Senior Cloud Specialist',
'Full-time . Singapore . 5 - 10 Years',
'12 days ago'],
['Cloud Native Developer',
'Full-time . Hyderabad . 2 - 5 Years',
'13 days ago']]
但是你最终会得到一个列表列表。如果要将其展平为字符串列表,请执行以下操作:
result = []
for el in d:
result = result + el
输出:
['Senior Cloud Specialist',
'Full-time . Singapore . 5 - 10 Years',
'12 days ago',
'Cloud Native Developer',
'Full-time . Hyderabad . 2 - 5 Years',
'13 days ago']
整体代码:
# original data
d = ['Senior Cloud Specialist\nFull-time · Singapore · 5 - 10 Years\n12 days ago',
'Cloud Native Developer\nFull-time · Hyderabad · 2 - 5 Years\n13 days ago']
# split by "\n"
d = [x.strip().split("\n") for x in d]
# flatten the list of lists into a list of strings
result = []
for el in d:
result = result + el
为什么会出现以下错误?
“AttributeError:‘list’对象没有‘strip’属性”
因为 strip 可以应用于str
(字符串)类型,并且您将它应用于列表。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句