python - 如何将具有 XY 坐标的 TXT 文件转换为点特征?
问题描述
我有一个格式如下的txt文件:
Spatial Reference: 43006
Name: Jones Tract
424564.620666, 4396443.55267
425988.30892, 4395630.01652
426169.09473, 4395426.63249
426214.291182, 4395268.4449
Name: Lewis Tract
427909.158152, 4393935.14955
428587.104939, 4393731.76552
428700.096071, 4393528.38148
428745.292523, 4393347.59567
Name: Adams Tract
424180.450819, 4393957.74778
424361.236629, 4393709.16729
424655.013571, 4393641.37261
424858.397607, 4393776.96197
我正在尝试将 Python 2.7 与 ArcPY 一起使用来从此文档中生成点 shapefile 或特征。我的 Python 技能充其量只是初学者,但我很享受学习它的挫败感。然而,这个问题让我很难过如何处理它。
我对此的理解是:
OPEN text file
READ LINES with NAME
GET INDEX of NAME
CREATE LIST from INDEX NAME to NAME-1
APPEND NAME to OBJECT in LIST
OUTPUT to SHAPEFILE
结果看起来与此类似:
NAME X Y
JONES 424564.620666 4396443.55267
JONES 425988.30892 4395630.01652
JONES 426169.09473 4395426.63249
JONES 426214.291182 4395268.4449
LEWIS 427909.158152 4393935.14955
LEWIS 428587.104939 4393731.76552
LEWIS 428700.096071 4393528.38148
LEWIS 428745.292523 4393347.59567
ADAMS 424180.450819 4393957.74778
ADAMS 424361.236629 4393709.16729
ADAMS 424655.013571 4393641.37261
ADAMS 424858.397607 4393776.96197
我尝试将信息捕获到列表中,并成功地将 XY 值隔离到其中,但不确定如何将信息移动到 shapefile 或表中。
# Opens txt file with tract data
iFile = open("U:/Data/Property_Module_7.txt", "r")
# Generates a list from the txt file
list1 = iFile.readlines()
# Returns index of lines with "Name"
for n, i in enumerate(list1):
if i.startswith("Name"):
print(n)
# lists for each coordinate between name indices
jones = list1[2:24]
smith = list1[25:43]
adams = list1[44:73]
jefferson = list1[74:90]
lewis = list1[91:108]
river = list1[109:]
print(adams)
iFile.close()
当我在 PyCharm 中运行它时,结果是这样的:
1
24
43
73
90
108
['424180.450819, 4393957.74778\n', '424361.236629,
4393709.16729\n', '424655.013571, 4393641.37261\n', '424858.397607, 4393776.96197\n', '425106.978096, 4394025.54246\n', '425287.763906, 4394048.14068\n', '425513.746168, 4394093.33714\n', '425762.326657, 4394115.93536\n', '426010.907146, 4394183.73004\n', '425943.112467, 4393889.9531\n', '425920.514241, 4393709.16729\n', '425852.719562, 4393641.37261\n', '425943.112467, 4393550.97971\n', '425852.719562, 4393257.20276\n', '425739.728431, 4393099.01518\n', '425626.7373, 4392895.63114\n', '425581.540847, 4392669.64888\n', '425423.353263, 4392443.66662\n', '425355.558585, 4392285.47904\n', '425129.576322, 4391969.10387\n', '424926.192286, 4392104.69323\n', '424677.611797, 4392330.67549\n', '424406.433082, 4392511.4613\n', '424225.647272, 4392692.24711\n', '424112.656141, 4392827.83647\n', '424112.656141, 4392963.42582\n', '424135.254367, 4393279.80099\n', '424180.450819, 4393957.74778\n', '\n']
另外,为什么列表的对象末尾有 \n ?
解决方案
这里有几个问题。首先,\n
换行符是文件中字符串的一部分。您可以使用该方法摆脱它们,该str.strip()
方法将删除前导和尾随空白字符。
其次,您关于查找Name
实例所在位置的逻辑是正确的。您正在尝试构建 csv,因此请以此为提示使用该csv
模块。
第三,确保使用with
上下文管理器进行文件处理,即使遇到错误也无需关闭文件
columns = ('Name', 'X', 'Y')
lines = []
with open('somefile.txt') as fh:
for line in fh:
if line.startswith('Name'):
# the name looks like it's always the second
# non-space string, so unpack it like so
_, name, *_ = line.split(' ')
# iterate over the next few lines until a blank is hit
while True:
try:
line = next(fh).strip()
except StopIteration:
break
if not line:
break
lines.append(dict(zip(columns, [name, *(x.strip() for x in line.split(','))])))
print(lines)
[{'Name': 'Jones', 'X': '424564.620666', 'Y': '4396443.55267'}, {'Name': 'Jones', 'X': '425988.30892', 'Y': '4395630.01652'}, {'Name': 'Jones', 'X': '426169.09473', 'Y': '4395426.63249'}, {'Name': 'Jones', 'X': '426214.291182', 'Y': '4395268.4449'}, {'Name': 'Lewis', 'X': '427909.158152', 'Y': '4393935.14955'}, {'Name': 'Lewis', 'X': '428587.104939', 'Y': '4393731.76552'}, {'Name': 'Lewis', 'X': '428700.096071', 'Y': '4393528.38148'}, {'Name': 'Lewis', 'X': '428745.292523', 'Y': '4393347.59567'}, {'Name': 'Adams', 'X': '424180.450819', 'Y': '4393957.74778'}, {'Name': 'Adams', 'X': '424361.236629', 'Y': '4393709.16729'}, {'Name': 'Adams', 'X': '424655.013571', 'Y': '4393641.37261'}, {'Name': 'Adams', 'X': '424858.397607', 'Y': '4393776.96197'}]
然后您可以使用 csv 模块将其写入文件,如下所示:
import csv
with open('someotherfile.csv', 'w') as fh:
writer = csv.DictWriter(fh, fieldnames = ['Name', 'X', 'Y'])
writer.writerow({c: c for c in columns})
writer.writerows(lines)
随着输出
cat someotherfile.csv
Name,X,Y
Jones,424564.620666,4396443.55267
Jones,425988.30892,4395630.01652
Jones,426169.09473,4395426.63249
Jones,426214.291182,4395268.4449
Lewis,427909.158152,4393935.14955
Lewis,428587.104939,4393731.76552
Lewis,428700.096071,4393528.38148
Lewis,428745.292523,4393347.59567
Adams,424180.450819,4393957.74778
Adams,424361.236629,4393709.16729
Adams,424655.013571,4393641.37261
Adams,424858.397607,4393776.96197
推荐阅读
- mysql - 为什么 MySQL substr 在长度超过 512 时返回数据类型 Text 中的值?
- python-3.x - 转换为不带括号的字符串
- sikuli - Sikulix:向右移动鼠标
- python - 在列表中查找连续的负值
- matplotlib - Matplotlib.odeint 为 acc = Gm/r**2 的简单公式提供错误解决方案
- css - 模拟第一次访问网页?
- typescript - 带有 toString() 的类可以用作字符串,但是如何说服 TypeScript 编译器呢?
- openstack - openstack 实例网络设置失败
- websocket - 为什么 JS 中的 WebSocket 在手机睡眠后从同一个选项卡连接失败但从另一个选项卡成功连接
- html - 接收不存在内容安全策略标头的内容安全策略错误