如何从python中的字符串中删除\ n

时间:2017-10-30 15:12:38

标签: python

我使用下面的代码来创建一个数据帧,但我得到'先生' \ n abc \ n D. \ n abc'的名称,我想摆脱\ n,请帮助

以下是我的代码:

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)

谢谢&问候, 桑杰

2 个答案:

答案 0 :(得分:3)

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

## Bla  abc  D.  blabla

这应该满足您的需求。

答案 1 :(得分:2)

如果要从字符串的开头和结尾删除空格,请使用:

const
  InstallShieldUpgradeCode = '{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}';

function InitializeSetup(): Boolean;
var
  WbemLocator, WbemServices, WbemObjectSet: Variant;
  Query: string;
  ProductCode: string;
begin
  WbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  WbemServices := WbemLocator.ConnectServer('.', 'root\CIMV2');
  Query :=
    'SELECT ProductCode FROM Win32_Property ' +
    'WHERE Property="UpgradeCode" AND Value="' + InstallShieldUpgradeCode + '"';
  WbemObjectSet := WbemServices.ExecQuery(Query);
  if not VarIsNull(WbemObjectSet) and (WbemObjectSet.Count > 0) then
  begin
    ProductCode := WbemObjectSet.ItemIndex(0).ProductCode;

    { Start uninstall here }
  end;

  Result := True;
end;

如果要从单词的中间删除换行符,请使用:

%K