如何从长字符串中提取字符串

时间:2019-05-27 16:56:35

标签: r python-3.x

我正在努力从长字符串中提取字符串,长字符串是使用python或R的csv文件的一行。长字符串是

"Zhao, Sihai@Univ Penn::Cai, T. Tony@Univ Penn::Li, Hongzhe@Univ Penn"

结果应该是

"Zhao, Sihai" "Cai, T. Tony" "Li, Hongzhe"

4 个答案:

答案 0 :(得分:1)

Python:

tmp_str = "Zhao, Sihai@Univ Penn::Cai, T. Tony@Univ Penn::Li, Hongzhe@Univ Penn"
names = [s.split("@")[0] for s in tmp_str.split("::")]
print(names)

输出为 ['Zhao, Sihai', 'Cai, T. Tony', 'Li, Hongzhe']

答案 1 :(得分:1)

在R中,您可以

a <- "Zhao, Sihai@Univ Penn::Cai, T. Tony@Univ Penn::Li, Hongzhe@Univ Penn"

sub('(^.+)@.*$', '\\1', unlist(strsplit(a, '::')))
#[1] "Zhao, Sihai"  "Cai, T. Tony" "Li, Hongzhe" 

答案 2 :(得分:0)

够了吗?
输入:

str = "Zhao, Sihai@Univ Penn::Cai, T. Tony@Univ Penn::Li, Hongzhe@Univ Penn"
parsed = x.replace('@Univ Penn','').split("::")
print(parsed)

输出:

['Zhao, Sihai', 'Cai, T. Tony', 'Li, Hongzhe']

答案 3 :(得分:0)

输入

 s = "Zhao, Sihai@Univ Penn::Cai, T. Tony@Univ Penn::Li, Hongzhe@Univ Penn"
l=s.replace('@Univ Penn','').split("::")
s=str(l)
s2=s[1:-1]
print(s2)

输出

'Zhao, Sihai', 'Cai, T. Tony', 'Li, Hongzhe'