简体   繁体   English

用文本文件中的字符串替换数字

[英]Replace number by string in text file

Given a string as input, I want to find and convert the integer elements of the line with the string num . 给定一个字符串作为输入,我想查找并转换字符串num为该行的整数元素。 Here is an example: 这是一个例子:

input: 输入:

"abc 123 def 1 in 1s and 100MB"

output: 输出:

"abc >num< def >num< in 1s and 100MB"

How can I do it? 我该怎么做?

That is how. 就是那样。

text_in = "abc 123 def 1 in 1s and 100MB"
text_out = ' '.join(['>num<' if x.isnumeric() else x for x in text_in.split()])
print(text_out)  # prints: abc >num< def >num< in 1s and 100MB

I took the liberty to assume that you can read the text from a file and can also write the new text to a file so I am skipping those parts. 我冒昧地假设您可以从文件中读取文本,也可以将新文本写入文件,因此我跳过了那些部分。

It all boils down to the use of isnumeric() . 归结为使用isnumeric()

Use a regular expression : 使用正则表达式

import re

s = "abc 123 def 1 in 1s and 100MB"

print(re.sub(r"\b\d+\b", ">num<", s))
# output: "abc >num< def >num< in 1s and 100MB"

The regular expression string r"\\b\\d+\\b" matches a character string that consists of at least one numeral character ( \\d+ ) enclosed by word boundaries ( \\b ). 正则表达式字符串r"\\b\\d+\\b"与包含至少一个由单词边界( \\b )包围的数字字符( \\d+ )组成的字符串匹配。 The \\b part also matches the beginning and the end of a string. \\b部分还匹配字符串的开头和结尾。

The call to re.sub() replaces all occurrences of a matching character string in the third argument s by the second argument ( ">num<" ). re.sub()的调用用第二个参数( ">num<" )替换了第三个参数s出现的所有匹配字符串。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM