-1

I ran selenium chromedriver and did scrap some text from website. Now the problem is text string is not in proper format. The string in the code is: selectDescription.text code:

for link in links:
    print "Navigating to link" + link
    browser.get(link)
    try:
        selectDescription = browser.find_element_by_xpath("(//div[@class='md'])[2]")
        try:
            data['description'].append(selectDescription.text)
        except KeyError:
            data['description'] = [selectDescription.text]
    except NoSuchElementException:
        try:
            data['description'].append("")
        except KeyError:
            data['description'] = [""]
        continue

For example:

this is is bad

text

I want it to be formatted into: this is bad text

How can I do that? Do I have to remove new line before I insert it into my array? I did try searching, but since I am new to python, some of the stuff is just getting over my head. Any help will be highly appreciated.

Should I be using replace method?

Vipul Mehra
  • 141
  • 1
  • 2
  • 8

2 Answers2

1

You can do this by calling "some string with \n linebreaks".replace("\n","")

Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23
  • 1
    Yes Sebastian, I was able to find this by my own after searching for an hour!! lol. Sometimes simple things are difficult to comprehend, especially when learning a new language. Thanks, your answer does work perfectly in this scenario. – Vipul Mehra Apr 28 '17 at 05:23
0

I am sorry to ask this question, but after simply using the replace function I was able to resolve this problem. The code might help some newbie trying their hands on python:

string.replace('\n', ' ')

In my scenario I did: selectDescription.text.replace('\n', ' ') Thanks again :)

Vipul Mehra
  • 141
  • 1
  • 2
  • 8