You need to enable JavaScript to run this app. Python实现含多井多表文本的各井SubZones=3总进尺计算
优惠活动
大模型
产品
解决方案
定价
更多

Python实现含多井多表文本的各井SubZones=3总进尺计算

最优实现方案

针对给定结构的井分层深度文本,提供两类适配不同场景的实现,均满足「逐段判断SubZones=3、累加进尺、输出结构化井名-总进尺表格」的核心需求:

方案1:纯Python内置实现(无第三方依赖)

适合快速跑数、不需要额外安装库的轻量场景,逻辑直接对应文本结构,兼容空行、省略号占位行、多空格分隔的格式。

well_total_footage = {}
current_well = None

for line in text.strip().splitlines():
    clean_line = line.strip()
    # 跳过空行、省略号占位行
    if not clean_line or set(clean_line) == {'.'}:
        continue
    # 识别井名行,更新当前跟踪的井
    if clean_line.startswith("Well name:"):
        current_well = clean_line.split(":", 1)[1].strip()
        well_total_footage[current_well] = 0.0
        continue
    # 跳过表头行
    if clean_line.startswith("MD in"):
        continue
    # 解析深度数据行
    md_in_str, md_out_str, sub_zone_str = clean_line.split()
    md_in = float(md_in_str)
    md_out = float(md_out_str)
    sub_zone = float(sub_zone_str)
    # 仅统计SubZones=3的分段,用精度阈值规避浮点数误差
    if abs(sub_zone - 3.0) < 1e-6:
        well_total_footage[current_well] += md_out - md_in

# 输出结构化Markdown表格
print("| 井名 | SubZones=3层位总进尺 |")
print("|------|----------------------|")
for well, total in well_total_footage.items():
    print(f"| {well} | {total:.4f} |")

方案1运行示例输出

井名SubZones=3层位总进尺
Well-0016.4640
Well-1004.4710

方案2:Pandas实现(适配拓展分析场景)

如果后续需要合并其他井数据、导出Excel/CSV、做多维度统计,用正则匹配的方案鲁棒性更强,能兼容制表符、多空格等不规则分隔格式。

依赖安装:pip install pandas tabulate(tabulate用于直接输出Markdown格式表格)

import pandas as pd
import re

# 预定义正则规则
well_pattern = re.compile(r"Well name:\s+(Well-\d+)")
data_pattern = re.compile(r"([\d.]+)\s+([\d.]+)\s+([\d.]+)")

records = []
current_well = None
for line in text.strip().splitlines():
    clean_line = line.strip()
    # 匹配井名
    well_match = well_pattern.match(clean_line)
    if well_match:
        current_well = well_match.group(1)
        continue
    # 匹配深度数据行
    data_match = data_pattern.match(clean_line)
    if data_match and current_well:
        md_in, md_out, sub_zone = map(float, data_match.groups())
        records.append([current_well, md_in, md_out, sub_zone])

# 数据处理:筛选SubZones=3的行,计算分段进尺,按井名汇总
df = pd.DataFrame(records, columns=["井名", "MD in", "MD out", "SubZones"])
result = (
    df[abs(df["SubZones"] - 3.0) < 1e-6]
    .assign(分段进尺=lambda x: x["MD out"] - x["MD in"])
    .groupby("井名", as_index=False)["分段进尺"]
    .sum()
    .rename(columns={"分段进尺": "SubZones=3层位总进尺"})
)

# 输出Markdown表格,也可直接导出为Excel:result.to_excel("层位进尺统计.xlsx", index=False)
print(result.to_markdown(index=False, floatfmt=".4f"))

关键兼容点说明

  • 浮点数判断不要直接用==匹配3.0,用1e-6精度阈值规避存储带来的精度误差(比如3.0000000001被误判为非3的情况)
  • 自动跳过文本中的省略号占位行、空行、表头行,不需要手动清洗文本
  • 累加逻辑自动覆盖单井多次进入SubZones=3层位的场景,不需要额外判断分段是否连续

内容的提问来源于stack exchange,提问作者Mohamad Alkhatib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.03 18:36:03