python读取坐标_Python从文本fi读取XY坐标

本文介绍如何使用Python从文本文件读取XY坐标,根据行中的数值数量将它们分别存储到多边形和矩形列表中。代码示例展示了如何处理文本文件,将8个数字的行转化为多边形,4个数字的行转化为矩形,并返回这两个列表。
摘要由CSDN通过智能技术生成

我有一个文本文件,其编号以以下格式存储:93 407 77 400 94 365 109 372

135 312 180 328

100 120 140 160

我想把这些值放到两个列表中。一个是多边形,一个是矩形。在

如果一条线有8个数字,则存储到多边形列表中。如果一行有4个数字,那么它将被存储在矩形列表中。像这样

^{pr2}$

然后我将使用这些值在画布上绘制矩形或多边形。在

到目前为止,我的代码是:class getXYCoords:

def __init__(self, textFile):

self.textFile = textFile

polygon = []

rectangle = []

with open(self.textFile) as f:

for line in f:

line = line.split() #strip EOL

if line: #lines (ie skip them)

# Stores values into polygon list

if len(line) == 8:

line = [int(i) for i in line]

polygon.append(line)

# Stores values into rectangle list

elif len(line) == 4:

line = [int(i) for i in line]

rectangle.append(line)

return polygon, rectangle

# Example program

if __name__ == '__main__':

#polygon = []

#rectangle = []

# Get XY coordinates

polygon, rectangle = getXYCoords('HAMH HUTC XY.txt')

print(polygon,'\n')

print(rectangle)

当我运行程序时,我收到以下错误消息:line 46 in module

polygon, rectangle = getXYCoords('HAMH HUTC XY.txt')

TypeError: init() should return None, not 'tuple'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值