如何获取文本文件每行开头的数字?
How do I get the numbers at the beginning of each line of a text file?
我有一个 .txt 文件,里面有很多行,即
230498soieung
3984sdgoij
032498eersn
如何访问此文本文件然后获取这些号码的列表,即
230498, 3984, 032498
如果我能在Python做到这一点,那就更好了。
使用re
模块操作文本,可以使用\d+
匹配多个数字,使用match()
从头搜索。
import re
lines = ['230498soieung', '3984sdgoij', '032498eersn']
[re.match('\d+', line).group() for line in lines]
输出:
['230498', '3984', '032498']
我有一个 .txt 文件,里面有很多行,即
230498soieung
3984sdgoij
032498eersn
如何访问此文本文件然后获取这些号码的列表,即
230498, 3984, 032498
如果我能在Python做到这一点,那就更好了。
使用re
模块操作文本,可以使用\d+
匹配多个数字,使用match()
从头搜索。
import re
lines = ['230498soieung', '3984sdgoij', '032498eersn']
[re.match('\d+', line).group() for line in lines]
输出:
['230498', '3984', '032498']