-2

I used this

print(listing_jobs)

resulted in this

['Senior Cloud Specialist\nFull-time · Singapore · 5 - 10 Years\n12 days ago', 'Cloud Native Developer\nFull-time · Hyderabad · 2 - 5 Years\n13 days ago']

How can I convert \n to comma?

When I use

listing_jobs.strip().split('\n')

it's showing error

AttributeError: 'list' object has no attribute 'strip'
rdas
  • 20,604
  • 6
  • 33
  • 46
jaz
  • 91
  • 3
  • 7

4 Answers4

2

If I understand your question well, you want to split the elements of the list listing_jobs by \n ? if so, you can use list comprehension as follows:

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]

That will give the following list of lists:

[['Senior Cloud Specialist',
  'Full-time . Singapore . 5 - 10 Years',
  '12 days ago'],
 ['Cloud Native Developer',
  'Full-time . Hyderabad . 2 - 5 Years',
  '13 days ago']]

But you will end up with a list of lists. If you want to flatten it to a list of strings, do the following:

result = []
for el in d:
     result = result + el 

output:

['Senior Cloud Specialist',
 'Full-time . Singapore . 5 - 10 Years',
 '12 days ago',
 'Cloud Native Developer',
 'Full-time . Hyderabad . 2 - 5 Years',
 '13 days ago']

Overall code:

# 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 

Why you have the following error?

"AttributeError: 'list' object has no attribute 'strip'"

Because strip can be applied on str (string) type and you are applying it on a list.

Mohamed Ali JAMAOUI
  • 14,275
  • 14
  • 73
  • 117
  • how i change the order according to the date of posting(Most old first) – jaz Apr 17 '19 at 12:56
  • You can apply a lambda to sort like this `sorted(result, key=lambda x: sort_by_date(x))`, before that you need to define a function `sort_by_date` in which you extract the date from string to a number and return it from the function so it used for sorting. – Mohamed Ali JAMAOUI Apr 17 '19 at 13:00
  • job_name details \ 0 Sr.QA Engineer Full-time · Hyderabad · 3 - 5 Years time 0 9 hours ago 1 Senior Cloud Specialist Full-time · Singapore · 5 - 10 Years 1 12 days ago 2 Cloud Native Developer Full-time · Hyderabad · 2 - 5 Years 2 13 days ago how i store the data according to the date of posting(Most old first) as a DataFrame in CSV – jaz Apr 17 '19 at 13:59
  • if the data is in a list you an do `pd.DataFrame(data)`, it it's a in a csv file, you can do `pd.read_csv("path_to_csv_file", sep=",")`. Hope that helps. – Mohamed Ali JAMAOUI Apr 17 '19 at 14:05
  • thanks for your help how i sort table data's based on time 9 hours ago,2 months ago in pandas – jaz Apr 17 '19 at 14:31
  • Here's an example on how to sort a dataframe based on date: https://stackoverflow.com/questions/28161356/sort-pandas-dataframe-by-date – Mohamed Ali JAMAOUI Apr 17 '19 at 14:33
  • but its date in my table its a string like 9 hours ago and 1 month ago etc..how i can sort based on this showing error ('Unknown string format:', '9 hours ago') – jaz Apr 17 '19 at 14:44
0

You need to iterate through your list of strings, and replace \n with a ,

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']
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40
0
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],[])
Preetham
  • 577
  • 5
  • 13
0

Try this.

for item in listing_jobs: print(item.replace('\n',','))