简体   繁体   English

如何从给定的字符串中只选择数字/数字并使用 python 正则表达式跳过文本?

[英]How to select only numbers/digits from a given string and skip text using python regex?

Given Strings:给定字符串:

57 years, 67 daysApr 30, 1789 57 岁零 67 天 1789 年 4 月 30 日

61 years, 125 daysMar 4, 1797 61 岁零 125 天 1797 年 3 月 4 日

57 years, 325 daysMar 4, 1801 57 年 325 天 1801 年 3 月 4 日

57 years, 353 daysMar 4, 1809 57 年 353 天 1809 年 3 月 4 日

58 years, 310 daysMar 4, 1817 58 年 310 天 1817 年 3 月 4 日

In regex101:在正则表达式 101 中:

Pattern = (?P<Years>[\d]{1,2}) years, (?P<Days>[\d]{1,3}) days(?P<Month>[\w]{3} [\d]{1,2}), (?P<Year>[\d]{4})模式 = (?P<Years>[\d]{1,2}) years, (?P<Days>[\d]{1,3}) days(?P<Month>[\w]{3} [\d]{1,2}), (?P<Year>[\d]{4})

Output: Output of Regex Pattern输出:正则表达式模式的输出

In Python(IDE : Jupyter Notebook) : Python Output Here it is showing only nan values in dataframe, how to solve this ?在 Python(IDE:Jupyter Notebook)中: Python 输出这里只显示数据框中的 nan 值,如何解决这个问题?

Use:利用:

#Preparing data
string = """57 years, 67 daysApr 30, 1789
61 years, 125 daysMar 4, 1797
57 years, 325 daysMar 4, 1801
57 years, 353 daysMar 4, 1809
58 years, 310 daysMar 4, 1817"""
df = pd.DataFrame(string.split('\n'))

#Solution
temp = df[0].str.extractall('(?P<Years>[\d]{1,2}) years, (?P<Days>[\d]{1,3}) days(?P<Month>[\w]{3} [\d]{1,2}), (?P<Year>[\d]{4})')

Output:输出:

        Years   Days    Month   Year
match               
0   0   57  67  Apr 30  1789
1   0   61  125 Mar 4   1797
2   0   57  325 Mar 4   1801
3   0   57  353 Mar 4   1809
4   0   58  310 Mar 4   1817

FYI, your code ran perfectly for me, maybe you have some whitespace issues in your dataframe:仅供参考,您的代码对我来说运行得很好,也许您的数据框中有一些空白问题:

import pandas as pd
import numpy as np

from io import StringIO

st = StringIO("""57 years, 67 daysApr 30, 1789

61 years, 125 daysMar 4, 1797

57 years, 325 daysMar 4, 1801

57 years, 353 daysMar 4, 1809

58 years, 310 daysMar 4, 1817""")

df = pd.read_csv(st, sep='\s\s\s+', header=None, engine='python')

Pattern = '(?P<Years>[\d]{1,2}) years, (?P<Days>[\d]{1,3}) days(?P<Month>[\w]{3} [\d]{1,2}), (?P<Year>[\d]{4})'

df[0].str.extract(Pattern)

Output:输出:

  Years Days   Month  Year
0    57   67  Apr 30  1789
1    61  125   Mar 4  1797
2    57  325   Mar 4  1801
3    57  353   Mar 4  1809
4    58  310   Mar 4  1817

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python regex - 从带有数字和字符的字符串中提取数字 - python regex - extracting digits from string with numbers and characters 我怎么能告诉一个字符串是否只包含使用正则表达式的python中的数字和空格 - how can i tell if a string contains ONLY digits and spaces in python using regex Python:如何使用正则表达式显示文本文件中的顶部数字 - Python: How to display the top numbers from text files using regex 在python中使用正则表达式从字符串中提取数字 - Extracting numbers from a string using regex in python Python:使用正则表达式从字符串中提取数字 - Python: extracting numbers from string using regex 如何使用来自不同国家的各种格式的正则表达式在 Python 中提取电话号码(仅限数字)? - How to extract phone numbers (numbers only) in Python using Regex from various formats from different countries? 如何在 python 中使用正则表达式跳过字符串中 2 个字符之间的字符? - how to skip the characters between 2 characters in a string using regex in python? 使用regex sub(Python)复制字符串中的数字 - Replication of digits in a string using regex sub (Python) python正则表达式:从字符串中获取结束数字 - python regex: get end digits from a string 如何从字符串中提取数字并使用正则表达式返回最后3个或更少 - How to extract digits from string and return the last 3 or less using regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM