You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Dart中读写.ini文件并将PaPo.GeoC坐标列表转换为目标结构

解决方案

你可以直接按以下步骤完成格式转换,用Python实现的代码示例如下:

  • 第一步:用configparser库读取ini文件,提取所有PaPo.X.GeoC格式的键值对
  • 第二步:拆分每个值的逗号分隔内容,分别对应纬度lat、经度long
  • 第三步:按你需要的目标格式拼接输出即可

完整代码示例

import configparser

# 读取ini文件
config = configparser.ConfigParser()
# 替换成你的实际ini文件路径
config.read('你的配置文件路径.ini', encoding='utf-8')

# 提取所有坐标
coords = []
# 遍历ini对应分区下的所有键,示例用默认DEFAULT分区,实际使用替换成你自己的分区名即可
for key in config['DEFAULT']:
    if key.startswith('PaPo.') and key.endswith('.GeoC'):
        lat, long = config['DEFAULT'][key].split(',')
        coords.append((float(lat.strip()), float(long.strip())))

# 按目标格式输出
output = "location:{\n"
for lat, long in coords:
    output += f"lat : {lat}\nlong : {long}\n"
output += "};"

print(output)
# 如果需要写入文件可以打开下面的注释,替换成实际输出路径
# with open('输出文件路径.txt', 'w', encoding='utf-8') as f:
#     f.write(output)

注意事项

  • 如果你给出的目标结构需要作为合法JS/JSON对象使用,注意同一个对象内不能出现重复的lat、long键,建议调整为数组格式location: [{lat: xxx, long: xxx}, ...]更符合通用规范,避免后续使用报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 18:45:05