简体   繁体   English

从.txt 导入字符串并搜索第一个数字字符 - python

[英]Import strings from .txt and searching for first numeric character - python

I am a total noob with python (programming in fact) but I hope you can help:)我是 python 的菜鸟(实际上是编程),但我希望你能提供帮助:)

I have a.txt file with a list of strings containing addresses.我有一个包含地址的字符串列表的 .txt 文件。

I want to import it to Python and then search for the first numberic character and then create a new column for it...like我想将它导入到 Python 然后搜索第一个数字字符,然后为它创建一个新列......就像

input输入

'Elm Street 12' 
'Baker Street 143'

and output和 output

'Elm Street 12' , 12
'Baker Street 143' , 14

and save it to.txt.并将其保存到.txt。

I am trying to do if from the windows cmd.我正在尝试从 windows cmd 开始。

Thank you in advance.先感谢您。

with open('file.txt') as inn:
    for line in inn:
        print "%s %s" % (line, [int(item) for item in line.split(' ') if item.isdigit()])

You probably want to use a dict .您可能想使用dict Loop through and use re to find your numeric characters, use that as a key in the dict to each string.循环并使用re查找您的数字字符,将其用作每个字符串的字典中的键。 If you expect to have duplicates.如果您希望有重复。

import re
results = {}
for s in ['Elm Street 12', 'Baker Street 143']:
    match = re.search(r'\d+', s)
    if match:
        results[match.group()] = s

>>> results
<<< {'12': 'Elm Street 12', '143': 'Baker Street 143'}

This might be what you are looking for这可能是您正在寻找的

import re

input = '''Elm Street 12
Baker Street 143'''
output = ""

rows = input.split("\n")
for row in rows:
    m = re.search(r'\d+', row)
    output += "{0} {1}\n".format(row, m.group())

print output
import re
regx = re.compile("^('.+ (\d+)')",re.MULTILINE)

with open('Copie de fileinput.txt','r+') as f:
    mod = regx.sub('\\1 , \\2',f.read())
    f.seek(0,0)
    f.write(mod)

Note that I understood that there are quotes ' in the file.请注意,我知道文件中有引号 ' 。 I doubt of that, but your output have quotes in it, so my regex has quotes.... You will remove thgem if there are no quotes in fact我对此表示怀疑,但是您的 output 中有引号,所以我的正则表达式有引号....如果实际上没有引号,您将删除 thgem

Intead of recording data in a flat file, you should better use the pickle module不要将数据记录在平面文件中,最好使用 pickle 模块

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

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