如何使用python将字符串拆分为引用的句子和数字

时间:2013-07-10 16:42:19

标签: python string split

大家好我是python的新手,非常感谢你的帮助!

我有这样的多个字符串:

21357.53 84898.10 Mckenzie Meadows Golf Course 80912.48 84102.38

我正在试图找出如何基于一组单词(即"Mckenzie Meadows Golf Course")分割线条及其周围的引号和双引号。

然后我将字符串重新排列为这种格式:

"Mckenzie Meadows Golf Course" 21357.53 84898.10 80912.48 84102.38

重新排列我只会使用

for row in data:
    outfile.write('{0} {1} {2} {3} {4}'.format(row[2], row[0], row[1], row[3], row[4]))
    outfile.write('\n')

但是我只是不确定如何从单行引出单句。谢谢你的帮助!

5 个答案:

答案 0 :(得分:2)

你可以试试这个:

s = "21357.53 84898.10 Mckenzie Meadows Golf Course 80912.48 84102.38"
sList = s.split(' ')
words = []
nums = []
for l in sList:
    if l.isalpha():
        words.append(l)
    elif l.isdigit():
        nums.append(l)

wordString = "\"%s\"" %  " ".join(words)
row = [wordString] + nums

此时,row包含您想要的行。

答案 1 :(得分:2)

这就是我这样做的方式:

import re

tgt='21357.53 84898.10 Mckenzie Meadows Golf Course 80912.48 84102.38'

nums=[m.group() for m in re.finditer(r'[\d\.]+',tgt)]
words=[m.group() for m in re.finditer(r'[a-zA-Z]+',tgt)]
print '"{}" {}'.format(' '.join(words),' '.join(nums))

打印:

"Mckenzie Meadows Golf Course" 21357.53 84898.10 80912.48 84102.38

或者,你可以测试Python认为是浮点数来找到它们:

nums=[]
words=[]
for e in tgt.split():
    try:
        nums.append(float(e))
    except ValueError:
        words.append(e)

print words,nums       

最后,如果你有固定格式的4个浮点数和一个字符串(float,float,string,float,float),你可以这样做:

li=tgt.split()
nums=' '.join(li[0:2]+li[-2:])
words=' '.join(li[2:-2])
print words,nums

答案 2 :(得分:1)

使用正则表达式的代码:

import re

s = '21357.53 84898.10 Mckenzie Meadows Golf Course 80912.48 84102.38'
row = re.search('([0-9.]+)\s([0-9.]+)\s([\w ]+)\s([0-9.]+)\s([0-9.]+)', s)
if row:
    print '"{0}" {1} {2} {3} {4}'.format(row.group(3), row.group(1), row.group(2), row.group(4), row.group(5))

将打印(带双引号):

 "Mckenzie Meadows Golf Course" 21357.53 84898.10 80912.48 84102.38

答案 3 :(得分:0)

使用str方法:

>>> s = '21357.53 84898.10 Mckenzie Meadows Golf Course 80912.48 84102.38'
>>> temp = s.split()
>>> temp
['21357.53', '84898.10', 'Mckenzie', 'Meadows', 'Golf', 'Course', '80912.48', '84102.38']
>>> row = [temp[0], temp[1], '"'+' '.join(temp[2:-2])+'"', temp[-2], temp[-1]]
>>> row
['21357.53', '84898.10', '"Mckenzie Meadows Golf Course"', '80912.48', '84102.38']
>>> print '{0} {1} {2} {3} {4}'.format(row[2], row[0], row[1], row[3], row[4])
"Mckenzie Meadows Golf Course" 21357.53 84898.10 80912.48 84102.38

答案 4 :(得分:0)

使用str方法,filterlambda

>>> words = "21357.53 84898.10 Mckenzie Meadows Golf Course 80912.48 84102.38".split()
>>> print '"%s" %s'%(" ".join(filter(lambda x: x.isalpha(), words)), " ".join(filter(lambda x: not x.isalpha(), words)))
"Mckenzie Meadows Golf Course" 21357.53 84898.10 80912.48 84102.38

更严格地说,不假设所有非alpha字都是浮点数(使用reduce):

>>> words = "21357.53 84898.10 Mckenzie Meadows Golf Course 80912.48 84102.38".split()
>>> print '"%s" %s'%(" ".join(filter(lambda x: x.isalpha(), words)), " ".join(filter(lambda x: reduce(lambda y, z: z.isdigit() and z, x.split('.'), True), words)))
"Mckenzie Meadows Golf Course" 21357.53 84898.10 80912.48 84102.38