我在一个excel列的行中有2000多个字符串。每个单元格都有一个遵循以下格式的字符串:
" BROOKDALE WOODSTOCK 1000 Professional Way伍德斯托克,GA 30188(678)341-5720 www.brookdale.com"
我想将此字符串拆分为4个不同的类别。我还想到了如何使用哪种字符串格式作为分隔符。
理想情况下,从上面的示例中,我有一个脚本可以将其转换为:
BROOKDALE WOODSTOCK,1000 Professional Way Woodstock,GA 30188,(678)341-5720,www.brookdale.com
有谁知道我怎么能做到这一点?我应该将其导出为.csv文件并在Python中执行此操作。有没有办法在Excel中完成这个任务?城市和州(伍德斯托克,乔治亚州)之间的字符串中预先存在的逗号是否会影响.csv文件?我该如何解决这个问题?
提前致谢。
答案 0 :(得分:1)
您的专线位于A1
,请使用这些公式
在B1
:=TRIM(LEFT(A1,FIND({"1","2","3","4","5","6","7","8","9","0"},A1)-1))
C1
:=TRIM(MID(A1,LEN(B1)+1,FIND("GA 30",A1)-LEN(B1)))
在D1
:=MID(A1,FIND("(",A1),13)
在E1
:=MID(A1,FIND("www",A1),999)
请注意,这假设GA 30
,(
和www
不会在字符串中的任何其他地方显示,而不是OP中的特定位置
答案 1 :(得分:1)
以下(粗略)大纲是您正在寻找的“天真”实现。但是:
这是:
import re
# Input
sample = "BROOKDALE WOODSTOCK 1000 Professional Way Woodstock, GA 30188 (678) 341-5720 www.brookdale.com"
# Split the line around its blank spaces
lst = sample.split(' ')
# Create an empty list for each desired category
part1 = []
part2 = []
part3 = []
part4 = []
# Now to put each element into the right category
while lst:
# Part 1
while lst:
e = lst.pop(0)
if not re.search("[0-9]", e):
part1.append(e)
else:
break
# Part 2
part2.append(e)
while lst:
e = lst.pop(0)
if not re.search("\(", e):
part2.append(e)
else:
break
# Part 3
part3.append(e)
while lst:
e = lst.pop(0)
if not re.search("w", e):
part3.append(e)
else:
break
# Part 4
part4 = [e] + lst
# For "proof of concept" purposes, join the fields and print them out
part1 = " ".join(part1)
part2 = " ".join(part2)
part3 = " ".join(part3)
part4 = " ".join(part4)
print("%s\n%s\n%s\n%s" % (part1, part2, part3, part4))
输出:
BROOKDALE WOODSTOCK
1000 Professional Way Woodstock, GA 30188
(678) 341-5720
www.brookdale.com
答案 2 :(得分:0)
如果您的2000个地址都遵循该格式,则可以使用正则表达式:
>>> import re
>>> s="BROOKDALE WOODSTOCK 1000 Professional Way Woodstock, GA 30188 (678) 341-5720 www.brookdale.com"
>>> pat=r'^([A-Z ]+)[ ](\d+[\w ]+,[ ]GA\s30\d\d\d)[ ](\(\d\d\d\)[ ]\d\d\d-\d\d\d\d)[ ](www[.\w]+)$'
>>> ', '.join(re.findall(pat, s)[0])
'BROOKDALE WOODSTOCK, 1000 Professional Way Woodstock, GA 30188, (678) 341-5720, www.brookdale.com'