如何将列表项换行符拆分为 python 中的逗号
How to split list items newline in to comma in python
我用过这个
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' object has no attribute 'strip'"
因为 strip 可以应用于 str
(字符串)类型,并且您正在将其应用于列表。
您需要遍历字符串列表,并将 \n
替换为 ,
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']
jobs = [job.replace('\n',',') for job in listing_jobs]
print(jobs)
#['Senior Cloud Specialist,Full-time · Singapore · 5 - 10 Years,12 days ago', #'Cloud Native Developer,Full-time · Hyderabad · 2 - 5 Years,13 days ago']
import re
list_of_lines = ['Senior Cloud Specialist\nFull-time · Singapore · 5 - 10 Years\n12 days ago', 'Cloud Native Developer\nFull-time · Hyderabad · 2 - 5 Years\n13 days ago']
just_replace = [re.sub('\n',",",lines) for lines in list_of_lines]
replace_then_split_to_list_of_lists = [re.sub('\n',",",lines).split(',') for lines in list_of_lines]
replace_and_then_split_to_flat_list = sum([re.sub('\n',",",lines).split(',') for lines in list_of_lines],[])
no_replace_split_flat_list = sum([lines.split('\n') for lines in list_of_lines],[])
试试这个。
listing_jobs 中的项目:
打印(item.replace('\n',','))
我用过这个
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' object has no attribute 'strip'"
因为 strip 可以应用于 str
(字符串)类型,并且您正在将其应用于列表。
您需要遍历字符串列表,并将 \n
替换为 ,
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']
jobs = [job.replace('\n',',') for job in listing_jobs]
print(jobs)
#['Senior Cloud Specialist,Full-time · Singapore · 5 - 10 Years,12 days ago', #'Cloud Native Developer,Full-time · Hyderabad · 2 - 5 Years,13 days ago']
import re
list_of_lines = ['Senior Cloud Specialist\nFull-time · Singapore · 5 - 10 Years\n12 days ago', 'Cloud Native Developer\nFull-time · Hyderabad · 2 - 5 Years\n13 days ago']
just_replace = [re.sub('\n',",",lines) for lines in list_of_lines]
replace_then_split_to_list_of_lists = [re.sub('\n',",",lines).split(',') for lines in list_of_lines]
replace_and_then_split_to_flat_list = sum([re.sub('\n',",",lines).split(',') for lines in list_of_lines],[])
no_replace_split_flat_list = sum([lines.split('\n') for lines in list_of_lines],[])
试试这个。
listing_jobs 中的项目: 打印(item.replace('\n',','))