仅从 Python 中的字符串中提取字符 [英] Extracting only characters from a string in Python

查看:35
本文介绍了仅从 Python 中的字符串中提取字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中,我只想从字符串中提取字符.

In Python, I want to extract only the characters from a string.

考虑我有以下字符串,

input = "{('players',): 24, ('year',): 28, ('money',): 19, ('ipod',): 36, ('case',): 23, ('mini',): 46}"

我想要的结果是,

output =  "players year money ipod case mini"

我试图只考虑字母来拆分,

I tried to split considering only the alphabets,

word1 = st.split("[a-zA-Z]+")

但是分裂并没有发生.

推荐答案

你可以用 re 来做,但是字符串拆分方法不需要正则表达式,它需要一个字符串.

You could do it with re, but the string split method doesnt take a regex, it takes a string.

这是使用 re 的一种方法:

Heres one way to do it with re:

import re
word1 = " ".join(re.findall("[a-zA-Z]+", st))

这篇关于仅从 Python 中的字符串中提取字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆