【问题标题】:Formatting Address into readable format based on Line Breaks根据换行符将地址格式化为可读格式
【发布时间】:2023-02-25 01:19:47
【问题描述】:

需要一些建议

我正在使用 Python 和 Selenium 抓取一个网站。报废结果正在保存到 CSV 文件中。

我目前正在抓取一家公司的地址,但是返回的地址正以以下格式传递到一个单元格中;

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

我已经实现了以下 sn-p,它删除了“空白”的“\n”

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

将地址重新格式化为这种格式。

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

但是,我只是想知道是否有一种方法可以让地址仍然重新调入一个单元格,但格式为 ;

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

【问题讨论】:

    标签: python selenium-webdriver


    【解决方案1】:

    ' ' 删除会导致文本编辑器/呈现器以其原始格式呈现字符串所需的信息丢失。它最终将整个文本呈现在一行中。

    您需要保持字符串中的换行符完好无损,并将其呈现为空白字符而不是可打印字符。

    将字符串分配给 python 中的变量并使用 print 函数打印它解释 ' ' 作为换行符。用 ' 保存文本是安全的 ' 完好无损的。呈现器将呈现标识 ' ' 作为换行符。

    str = "Catalyst Inc
    Bay Road
    Londonderry
    County Londonderry
    BT48 7TG
    BT 4 8 7 T G
    UNITED KINGDOM"`
    print (str)
    
    Catalyst Inc
    Bay Road
    Londonderry
    County Londonderry
    BT48 7TG 
    B T 4 8 7 T G
    UNITED KINGDOM
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      相关资源
      最近更新 更多