如何将带有XY坐标的TXT文件转换为点特征?

2024-06-08 23:10:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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

我尝试使用python2.7和ArcPY从这个文档生成一个点形状文件或特性。我的Python技能充其量是初学者,但我很享受学习它的挫折感。然而,这个问题让我纠结于如何处理它。在

我的理解是:

^{pr2}$

结果与此类似:

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或table中。在

# 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?在


Tags: 文件nametxt信息列表forwithfile
1条回答
网友
1楼 · 发布于 2024-06-08 23:10:37

这里有几个问题。首先,\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模块将其写入如下文件:

^{pr2}$

有了输出

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

相关问题 更多 >