Python 从本地文本文件中刮取基本值

Python 从本地文本文件中刮取基本值,python,excel,web-scraping,screen-scraping,extract,Python,Excel,Web Scraping,Screen Scraping,Extract,我想从下面的字符串中提取值。源是一个本地文本文件。最简单的解决方案是什么。假设编程诀窍最少:) 这应该会让您对以下步骤有一些了解: # Open the local file fo = open(file-name) # read the file - this assumes it is the first line line = fo.readline() # close the file fo.close() # Use a regular expression to find t

我想从下面的字符串中提取值。源是一个本地文本文件。最简单的解决方案是什么。假设编程诀窍最少:)


这应该会让您对以下步骤有一些了解:

# Open the local file
fo = open(file-name)

# read the file - this assumes it is the first line
line = fo.readline() 

# close the file
fo.close()

# Use a regular expression to find the specific groups
import re
mos = re.finditer(r"value=\\'([\d.]+)\\'", line)

for m in mos:
    print m.group(1)
给出:

48644.54
47912.02
52219.28
49854.88
re.finditer
返回的
mos
使我们能够迭代
match
对象,这就是
for
循环所做的。
match
对象中感兴趣的方法(函数)是
group()
,它返回每个括号组中的数据,这些数据在
()
中匹配

是否希望此表单中的循环取决于之后对数据的处理方式

正则表达式分解如下:

r”“
始终将原始字符串与正则表达式一起使用,这样更安全

value=\\'\\'
请注意,需要两个\字符。\是一个特殊字符,但添加额外的\将删除其特殊含义


([\d.]+)
括号将匹配此模式的数据分组。
[\d.]+
的意思是“一个或多个数字(数字)或点”。

您想读取文件的内容吗?如果源文件是本地文本文件,那么问题是什么,为什么在这个问题上有web和screen scraping标记?您希望提取哪一部分?请提供准确的预期输出。
48644.54
47912.02
52219.28
49854.88