2012-08-13 86 views
0

我有一个大的文本文件,其中包含数字和两种类型的字符,分别是'|'和。我在StackOverflow上搜索并找到了如何使用这个字符串,只保留字母。例如,如果Python:只保留字符串中的每个字符,每个都在换行

old_fruits='apple|0.00|kiwi|0.00|0.5369|-0.2437|banana|0.00|pear' 

然后

re.sub("[^A-Za-z]","",old_fruits) 

将返回

'applekiwibananapear' 

我试图写这些话拿出来与每行一个字一个文件,随后换行然后是下一个单词,如:

apple 
kiwi 
banana 
pear 

任何想法或指向正确的方向表示赞赏。

回答

1

试试这个:

import re 

old_fruits = 'apple|0.00|kiwi|0.00|0.5369|-0.2437|banana|0.00|pear' 

with open('fruits.out', 'w') as f: 
    fruits = re.findall(r'[^\W\d]+', old_fruits) 
    f.write('\n'.join(fruits)) 
+0

谢谢!我试过了,它像我希望的那样工作。 – Levar 2012-08-13 03:49:29

0

使用OP的代码为基础:

import re 
old_fruits = 'apple|0.00|kiwi|0.00|0.5369|-0.2437|banana|0.00|pear' 

with open('outdata.txt', 'w') as f: 
    f.write('\n'.join(re.sub("[^A-Za-z]"," ",old_fruits).split())) 

apple 
kiwi 
banana 
pear 

文件'outdata.txt'

+0

你刚刚把\ n纳入,拆分(取出\ n),然后把它们放回... – 2012-08-13 03:39:56

+0

为什么不是我的答案?使用+表示1+实例将被匹配并替换为\ n。感觉像这就是你想要做的 – 2012-08-13 03:42:39

0

答案并不难,虽然我不知道这是不是最好的做法,为什么不

print re.sub("[^A-Za-z]+","\n",old_fruits) #re.sub("[^A-Za-z]+","\n",old_fruits) is the string you want 

的“+”表示1+的非字母字符的实例将被替换与\ n

0
of=old_fruits.split("|") 
for i in range(0,len(of),2): 
# write to file 
1

你可以做到这一点,而无需使用正则表达式。分割字符串在管字符,用生成器表达式和inbuild string.isalpha()函数滤除那些仅是字母字符的单词,并一同加入,以形成最终输出:

old_fruits = 'apple|0.00|kiwi|0.00|0.5369|-0.2437|banana|0.00|pear' 
words = (word for word in old_fruits.split('|') if word.isalpha()) 
new_fruits = '\n'.join(words) 

print(new_fruits) 

输出是

apple 
kiwi 
banana 
pear 

根据需要(不写入文件,但我认为你能够应付这种情况)。

编辑:敲了一个快速的脚本来提供正则表达式的与非正则表达式的一些时间比较:

import timeit 

# Setup - not counted in the timing so it doesn't matter we include regex for both tests 
setup = r"""old_fruits = 'apple|0.00|kiwi|0.00|0.5369|-0.2437|banana|0.00|pear' 
import re 
fruit_re=re.compile(r'[^\W\d]+') 
""" 

no_re = r"""words = (word for word in old_fruits.split('|') if word.isalpha()) 
new_fruits = '\n'.join(words)""" 

with_re = r"""new_fruits = '\n'.join(fruit_re.findall(old_fruits))""" 

num = 10000 

print("Short input") 
t = timeit.timeit(no_re, setup, number=num) 
print("No regex: {0:.2f} microseconds to run".format((t*1e6)/num)) 
t = timeit.timeit(with_re, setup, number=num) 
print("With regex: {0:.2f} microseconds to run".format((t*1e6)/num)) 

print("") 
print("100 times longer input") 

setup = r"""old_fruits = 'apple|0.00|kiwi|0.00|0.5369|-0.2437|banana|0.00|pear'*100 
import re 
fruit_re=re.compile(r'[^\W\d]+')""" 

t = timeit.timeit(no_re, setup, number=num) 
print("No regex: {0:.2f} microseconds to run".format((t*1e6)/num)) 
t = timeit.timeit(with_re, setup, number=num) 
print("With regex: {0:.2f} microseconds to run".format((t*1e6)/num)) 

我的计算机上的结果:

Short input 
No regex: 18.31 microseconds to run 
With regex: 15.37 microseconds to run 

100 times longer input 
No regex: 793.79 microseconds to run 
With regex: 999.08 microseconds to run 

所以预编译对于较短的输入字符串,正则表达式更快,对于较长的输入字符串,生成器表达式更快(至少在我的计算机上 - Ubuntu Linux,Python 2.7 - 结果可能因您而异)。

+0

谢谢!这也很好。 – Levar 2012-08-13 04:00:06

+1

@Levar - 更新答案做一个快速测试的正则表达式与发电机的速度。 – Blair 2012-08-13 04:10:46

相关问题