在python中使用字节

0 python processing byte

我正在尝试将一个processing.org应用程序移植到python中并且遇到一些困难.

我需要在python中写这个:

int[][] elevation; // elevations in meters
float[][] felevation; // scaled to range [0,1] where 1=max
int maxheight;

void setup(){
size(600,600,P2D);

// read srtm binary file
elevation=new int[1201][1201];
felevation=new float[1201][1201];
byte b[] = loadBytes("N30W091.hgt"); // THIS IS A BINARY FILE
int ix=0;
maxheight=0;
for (int row=0;row<1201;row++) {
  for (int col=0;col<1201;col++) {
    // bytes are signed, from -128 to 127, converts to unsigned...
    int hi = b[ix] & 0xff; 
    int lo = b[ix+1] & 0xff; 
    int el=(int)((hi<<8)|lo); // big endian!
    elevation[row][col]=el;
    if (el>maxheight && el<32000) maxheight=el; 
    ix+=2;
   }
}
Run Code Online (Sandbox Code Playgroud)

... 等等

到目前为止我所做的是这样的:

elevation = [[],[]]
maxheight=0

b = open("C:\\Users\\CNA\\sketchbook\\_SRTM\\data\\N59E010.hgt","rb")
fin = b.read(1)
print(len(fin))
ix = 0
for row in range(0,1201):
    for col in range(0,1201):
        hi = (fin[ix]   + 0xff)
        lo = (fin[ix+1] + 0xff)
Run Code Online (Sandbox Code Playgroud)

我总是得到

Traceback (most recent call last):

  File "C:\Users\CNA\workspace\Revitter\PatternAsignment.py", line 16, in <module>

TypeError: unsupported operand type(s) for +: 'str' and 'int'
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?..我是python的新手,我没有使用字节的经验...

Kar*_*tel 5

惯用翻译将以完全不同的方式运作.

在原始代码中,您需要进行一些比特操作才能将两个字节值转换为单个数值.在Python中,有内置的功能:使用struct模块.事实证明,该模块已经构建为一次读取多个值.

此外,使用正斜杠作为文件名 - 它更容易,并保证工作.使用with-block确保文件自动正确关闭,并使用列表解析来简化循环 - 停止尝试告诉Python如何构建列表,并只询问所需的列表.

这给了我们:

import struct
with open('C:/Users/CNA/sketchbook/_SRTM/data/N59E010.hgt', 'rb') as data:
    elevation = [
        list(struct.unpack('>1201H', data.read(1201 * 2)))
        for row in range(1201)
    ]
maxheight = max(max(cell for cell in row if cell < 32000) for row in elevation)
Run Code Online (Sandbox Code Playgroud)

你完成了 欢迎使用Python :)