0

Need some advice

I am scraping a website using Python and Selenium. The results of the scrap are being saved into a CSV.

I am currently scraping the address of a company, however the address being returned back is being passed in the following format into one cell;

Catalyst Inc\nBay Road\nLondonderry\nCounty Londonderry\nBT48 7TG\nBT 4 8 7 T G\nUNITED KINGDOM

I have implemented the following snippet which removes "\n" for "Blanks"

df['full_address'] = df['full_address'].replace(r'\n',' ', regex=True)

Which reformats the address into this format.

Catalyst Inc Bay Road Londonderry County Londonderry BT48 7TG B   4 8 7 T G UNITED KINGDOM

However, I am just wondering is there a way I can get the address to still be retuned into one cell but be formatted as ;

Catalyst Inc
Bay Road
Londonderry
County Londonderry
BT48 7TG 
B T 4 8 7 T G
UNITED KINGDOM
MattDMo
  • 100,794
  • 21
  • 241
  • 231
Masond3
  • 111
  • 6
  • Does this answer your question? [CSV in Python adding an extra carriage return, on Windows](https://stackoverflow.com/questions/3191528/csv-in-python-adding-an-extra-carriage-return-on-windows) – JeffC Feb 25 '23 at 05:47

1 Answers1

0

'\n' removal causes a loss of information required by text editors/renders to render the string in its original format. It ends up rendering the whole text on a single line.

You need to keep the newline character within the string intact and render it as a whitespace character instead of a printable character.

Assigning the string to a variable in python and printing it using the print function interprets the '\n' as a new line character. It is safe to save the text with '\n' intact. The renderer will render the string identifying the '\n' as a new line character.

str = "Catalyst Inc\nBay Road\nLondonderry\nCounty Londonderry\nBT48 7TG\nBT 4 8 7 T G\nUNITED KINGDOM"`
print (str)

Catalyst Inc
Bay Road
Londonderry
County Londonderry
BT48 7TG 
B T 4 8 7 T G
UNITED KINGDOM
nitinsj
  • 11
  • 3