首页 > 解决方案 > 如何将字符串分解为多个换行符?

问题描述

我有亚马逊订单的详细信息,文字字符串由换行符 \n 分隔。

str = 'Linon Hampton Stool Fabric Top, 24-inch \nSold by: Amazon.com Services, Inc \n$33.99'

我想为每个新行将其拆分为 n 个字符串,使其看起来像。

str1 = 'Linon Hampton Stool Fabric Top, 24-inch'
str2 = 'Sold by: Amazon.com Services, Inc '
str3 = '$33.99'

标签: pythonstring

解决方案


使用split. 它将以列表的形式为您提供输出:

string = 'Linon Hampton Stool Fabric Top, 24-inch \nSold by: Amazon.com Services, Inc \n$33.99'

string.split("\n")

并且不要给你的变量 name str。它是一个内置函数名称。


推荐阅读