1

Am using the below code to create a dataframe but am getting 'Mr. \n abc \n D. \n abc' for name , i want to get rid of \n, please help

Below is my code:

names = []
orgs = []

for name in all_table_info.find_all('td', 
           class_="views-field views-field-field-lastname active"):
    names.append(name.text.strip() if name.text else None)

for organization in all_table_info.find_all('td', 
           class_="views-field views-field-field-employer"):
    orgs.append(organization.text.strip() if organization.text else None) 

df = pd.DataFrame({'Name' : names, 'Org' : orgs})

print (df)

Thanks & Regards, Sanjay

cs95
  • 379,657
  • 97
  • 704
  • 746
K. Sanjay
  • 125
  • 1
  • 11
  • If you have a string just do str.strip('\n') / str.strip('\n', '') https://stackoverflow.com/questions/9347419/python-strip-with-n – bhow Oct 30 '17 at 15:14
  • strip only removes leading and trailing characters, not mid-string. :/ – Uvar Oct 30 '17 at 15:19
  • @Uvar whoops, that was a typo. Meant to do strip('\n') and replace('\n', '') – bhow Oct 30 '17 at 15:30
  • Please `print(df.head(10))` and edit your question with the output. I'll be easier to figure out what you need. – cs95 Oct 30 '17 at 20:45
  • Name org Mr. \n Rex \n D. \n Adams Invesco Ltd. Mr. \n William \n S. \n Allen Macy's Inc. Mr. \n R. \n Wayne \n Anderson Amoco Corporation Prof. \n Chris \n Argyris (1923-2013) Monitor Company Marcia \n J. \n Avedon, \n Ph.D. Ingersoll Rand, plc. Here i want to remove (Full stops and '\n') – K. Sanjay Oct 31 '17 at 08:07
  • @K.Sanjay Add it to your question, I can't understand it in the comments. – cs95 Oct 31 '17 at 12:17

2 Answers2

3
mystring = "Bla \n abc \n D. \n blabla"
print(mystring.replace("\n", ""))

## Bla  abc  D.  blabla

That should serve your needs.

Uvar
  • 3,372
  • 12
  • 25
  • I have tried the below code but it is throwing an error "none type object is not callable", here is my code for name in all_table_info.find_all('div', class_="views-field views-field-view"): names.append(name.text.strip() if name.text else None) print(name.replace("\n", "")) – K. Sanjay Oct 30 '17 at 15:28
  • 1
    Don't name your variable `str`. – Brad Solomon Oct 30 '17 at 15:32
  • I tried like this ,print(str.replace("\n", "")) , but is is throwing a TypeError: replace() takes at least 2 arguments (1 given) – K. Sanjay Oct 30 '17 at 15:41
  • @BradSolomon you are right..I should not overwrite a class. :) – Uvar Oct 30 '17 at 16:03
  • @K.Sanjay as you can see by the comma separation, it has 2 arguments. If your interpreter faults you for giving only one, you probably made a typo somewhere. – Uvar Oct 30 '17 at 16:04
2

If you want to remove whitespace from the beginning and end of a string, use:

'  word  \n'.strip()  # -> 'word'

If you want to remove newlines from the middle of a word, use:

'a \n b \n c'.replace('\n', '')  # -> 'a  b  c'
Ben
  • 5,952
  • 4
  • 33
  • 44