How to retrieve human readable data from MMA8451 accelerometer using nodejs - node.js

I receive via serial port data from an accelerometer module MMA8451.
I'm using nodejs to capture data from serial and do the parsing work.
The payload I get seems to be an hex
d40 14fc 88c0
540 800 c8c0
840 340 20c0
b0fc c80 d8c0
200 700 68c0
800 480 acc0
100 c0 c0c0
Which I suppose are the three axis values
If I simply try to convert in binary and the decimal using hex2bin and parseint I get values that are ranging from 0 to 256 but they don't make sense.
XYZ 248 16 184
XYZ 252 64 192
XYZ 128 64 192
XYZ 0 192 192
XYZ 252 64 192
XYZ 0 192 192
XYZ 64 0 192
XYZ 0 128 192
XYZ 128 64 192
XYZ 0 0 192
How I can get human readable values from this data?
UPDATE:
As NikaTheEngineer pointed out I've looked at the accelerometer datasheet https://www.nxp.com/docs/en/data-sheet/MMA8451Q.pdf (see chapter 5.2) and the data I'm receiving seems to be the raw data from those registers OUT_X_MSB, OUT_X_LSB, OUT_Y_MSB, OUT_Y_LSB, OUT_Z_MSB
Those are 14bit so this imply some sort of data manipulation with by bites shift am I correct?
If I print the stream directly from the serialport (removing the hex conversion) I receive this kind of payload
<Buffer 65 33 38 30 20 36 38 66 63 20 34 38 30 20 61 38 63 30 20 31 30 38 33 0d 0a 65 33 38 30 20 37 34 66 63 20 39 38 30 20 61 38 63 30 20 31 30 38 34 0d 0a>
<Buffer 65 33 38 30 20 38 66 63 20 35 63 30 20 37 30 63 30 20 31 30 38 35 0d 0a>
<Buffer 65 33 38 30 20 61 63 66 63 20 63 34 30 20 63 38 63 30 20 31 30 38 36 0d 0a>
<Buffer 65 33 38 30 20 37 30 66 63 20 37 38 30 20 37 30 63 30 20 31 30 38 37 0d 0a>
<Buffer 65 33 38 30 20 36 30 66 63 20 36 38 66 63 20 66 34 63 30 20 31 30 38 38 0d 0a>
<Buffer 65 33 38 30 20 63 38 66 63 20 61 30 30 20 37 30 63 30>
<Buffer 20 31 30 38 39 0d 0a 65 33 38 30 20 30 66 63 20 37 38 30 20 62 63 63 30 20 31 30 39 30 0d 0a 65 33 38 30 20 37 34 66 63 20 34 30 30 20 61 38 63 30 20 ... 6 more bytes>
<Buffer 65 33 38 30 20 35 30 66 63 20 34 63 30 20 35 38 63 30 20 31 30 39 32 0d 0a>
<Buffer 65 33 38 30 20 38 63 66 63 20 37 38 30 20 37 38 63 30 20 31 30 39 33 0d 0a>
<Buffer 65 33 38 30 20 36 30 66 63 20 38 30 30 20 62 30 63 30 20 31 30 39 34 0d 0a>
<Buffer 65 33 38 30 20 32 38 66 63 20 33 30 30 20 37 38 63 30 20 31 30 39 35 0d 0a 65 33 38 30 20 64 38 66 63 20 37 30 30 20 37 30 63 30 20 31 30 39 36 0d 0a>
<Buffer 65 33 38 30 20 34 30 66 63 20 61 30 30 20 64 30 63 30 20 31 30 39 37 0d 0a 65 33 38 30 20 39 63 66 63 20 37 38 30 20 37 30 63 30 20 31 30 39 38 0d 0a ... 25 more bytes>
<Buffer 65 33 38 30 20 62 63 66 63 20 36 34 30 20 39 38 63 30>
<Buffer 20 31 31 30 30 0d 0a 65 33 38 30 20 34 38 66 63 20 36 38 30 20 38 30 63 30 20 31 31 30 31 0d 0a 65 33 38 30 20 34 66 63 20 32 63 30 20 61 30 63 30 20 ... 6 more bytes>
<Buffer 65 33 38 30 20 34 38 66 63 20 37 38 30 20 36 38 63 30 20 31 31 30 33 0d 0a 65 33 38 30 20 32 38 66 63 20 61 30 30 20 61 30 63 30 20 31 31 30 34 0d 0a>
<Buffer 65 33 38 30 20 35 30 66 63 20 39 38 30 20 38 30 63 30 20 31 31 30 35 0d 0a>
<Buffer 65 33 38 30 20 36 34 66 63 20 31 63 30 20 37 30 63 30 20 31 31 30 36 0d 0a 65 33 38 30 20 33 30 66 63 20 38 38 30 20 61 30 63 30 20 31 31 30 37 0d 0a>
<Buffer 65 33 38 30 20 38 66 63 20 62 38 30 20 35 34 63 30 20 31 31 30 38 0d 0a 65 33 38 30 20 64 34 66 63 20 63 30 30 20 64 30 63 30 20 31 31 30 39 0d 0a 65 ... 48 more bytes>
<Buffer 65 33 38 30 20 66 38 66 63 20 37 38 30 20 34 34 63 30 20 31 31 31 32 0d 0a 65 33 38 30 20 31 30 66 63 20 38 30 30 20 38 38 63 30 20 31 31 31 33 0d 0a ... 49 more bytes>
<Buffer 65 33 38 30 20 66 30 66 63 20 35 30 30 20 38 38 63 30 20 31 31 31 36 0d 0a 65 33 38 30 20 34 38 66 63 20 34 34 30 20 61 38 63 30 20 31 31 31 37 0d 0a ... 25 more bytes>
<Buffer 65 33 38 30 20 32 38 66 63 20 38 63 30 20 62 38 63 30 20 31 31 31 39 0d 0a 65 33 38 30 20 32 38 66 63 20 36 38 30 20 39 30 63 30 20 31 31 32 30 0d 0a>
<Buffer 65 33 38 30 20 65 38 66 63 20 66 38 30 20 32 38 63 30 20 31 31 32 31 0d 0a>
<Buffer 65 33 38 30 20 63 38 66 63 20 39 30 30 20 64 34 63 30 20 31 31 32 32 0d 0a>
<Buffer 65 33 38 30 20 35 34 66 63 20 36 34 30 20 66 38 63 30 20 31 31 32 33 0d 0a 65 33 38 30 20 38 30 66 63 20 37 30 30 20 33 38 63 30 20 31 31 32 34 0d 0a ... 50 more bytes>
<Buffer 65 33 38 30 20 35 63 66 63 20 32 30 30 20 63 63 63 30 20 31 31 32 37 0d 0a>
<Buffer 65 33 38 30 20 64 38 66 63 20 35 38 30 20 36 30 63 30 20 31 31 32 38 0d 0a 65 33 38 30 20 66 30 66 63 20 38 30 30 20 61 38 63 30 20 31 31 32 39 0d 0a>
<Buffer 65 33 38 30 20 65 30 66 63 20 64 63 30 20 62 30 63>
<Buffer 30 20 31 31 33 30 0d 0a 65 33 38 30 20 36 30 66 63 20 36 63 30 20 39 38 63 30 20 31 31 33 31 0d 0a>
<Buffer 65 33 38 30 20 33 30 66 63 20 35 30 30 20 36 30 63 30 20 31 31 33 32 0d 0a>
<Buffer 65 33 38 30 20 62 30 66 63 20 64 30 30 20 63 34 63 30 20 31 31 33 33 0d 0a>
I guess this is the stream of raw data in bytes.
I suppose the first step is to distinguish every single chunk samples composed by xmsb-xlsb ymsb-ylsb zmsb-zlsb, but at this point I have no idea how to do it, there should be some kind of delimiter?
I need to work blindly on the buffer just trusting the datastructure pointed in the datasheet?

First of all, you do not need to convert hex number to binary. Function parseInt can convert hex to decimal.
Check this out!
Secondly, if you parsed your data correctly, it should not be in range(0, 256); The last hex number you recived is c0c0, which in decimal is 49344 (unsigned value).
Go ahead and test it on this site!
Lastly, are you sure you are reading data from hardware correctly? I found the datasheet of the accelerometer and it is written there that data is stored in 14 bits for each axis. The highest unsigned value in hex that can be stored in 14 bits is 3FFF, which is far less than some of your data; for example: c0c0, acc0...
You can find the datasheet here!
If u let me know how u read data from the hardware, I might be able to help you :)
Good luck!

Related

Converting a date/time column from binary data type to the date/time data type using PySpark

I have a data frame with date/time column storing data as binary data type. I need to convert it the actual datetime data type so I can run SQL window functions and more. Hence, looking for some working examples.
Input dataframe schema:
root
|-- ce_time: binary (nullable = true)
Sample data:
+-------------------------------------------------------------------------------------+
|ce_time |
+-------------------------------------------------------------------------------------+
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 34 3A 33 39 2E 32 30 34 36 37 38 35 5A]|
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 34 3A 34 36 2E 38 32 33 32 34 32 5A] |
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 34 3A 35 34 2E 34 35 39 30 34 33 37 5A]|
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 35 3A 30 32 2E 35 37 30 38 35 39 36 5A]|
+-------------------------------------------------------------------------------------+
I can convert the above to string, it looks like this, but I need it in date/time type, not string.
+-------------------------------------------------------------------------------------+----------------------------+
|ce_time |ce_time_string |
+-------------------------------------------------------------------------------------+----------------------------+
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 34 3A 33 39 2E 32 30 34 36 37 38 35 5A]|2022-05-02T00:04:39.2046785Z|
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 34 3A 34 36 2E 38 32 33 32 34 32 5A] |2022-05-02T00:04:46.823242Z |
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 34 3A 35 34 2E 34 35 39 30 34 33 37 5A]|2022-05-02T00:04:54.4590437Z|
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 35 3A 30 32 2E 35 37 30 38 35 39 36 5A]|2022-05-02T00:05:02.5708596Z|
+-------------------------------------------------------------------------------------+----------------------------+
If someone knows how to convert binary to date/time in PySpark and keep the above date/time values exactly the same, please share!
Much appreciated!!
You can first convert your binary value to string value using decode built-in function and then convert this string value to timestamp value using cast column method
Note: timestamp is spark equivalent of python's datetime type
Here is the complete code:
from pyspark.sql import functions as F
result = df.withColumn('ce_time', F.decode('ce_time', 'UTF-8').cast('timestamp'))
With the following df input dataframe:
+-------------------------------------------------------------------------------------+
|ce_time |
+-------------------------------------------------------------------------------------+
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 34 3A 33 39 2E 32 30 34 36 37 38 35 5A]|
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 34 3A 34 36 2E 38 32 33 32 34 32 5A] |
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 34 3A 35 34 2E 34 35 39 30 34 33 37 5A]|
|[32 30 32 32 2D 30 35 2D 30 32 54 30 30 3A 30 35 3A 30 32 2E 35 37 30 38 35 39 36 5A]|
+-------------------------------------------------------------------------------------+
You get the following result dataframe:
+--------------------------+
|ce_time |
+--------------------------+
|2022-05-02 02:04:39.204678|
|2022-05-02 02:04:46.823242|
|2022-05-02 02:04:54.459043|
|2022-05-02 02:05:02.570859|
+--------------------------+
having the following schema:
root
|-- ce_time: timestamp (nullable = true)

How to generate Pre-signed PUT URL (if possible) using CloudFront signer with Python Boto client?

I have been working on S3 storage application using python boto client. Client requests pre-signed upload (Pre-signed PUT) and download (Pre-signed GET) URLs from server for files.
Using boto3 s3 session, this can be done using
response = session.generate_presigned_url(
"put_object",
Params={
"Bucket": client.aws_bucket,
"Key": s3_object,
},
ExpiresIn=client.url_expiration,
)
However, now I am trying to achieve the same using CloudFront apis of boto3. I followed this example to generate a download URL. (Keys are setup in aws console as documentation suggested).
If I upload using s3 pre-signed PUT URL, I cannot download the file generated via CloudFront pre-signed URL. It leads to the following error (some hash values have been changed to hide details):
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<AWSAccessKeyId>removed access key</AWSAccessKeyId>
<StringToSign>AWS4-HMAC-SHA256 20200909T010545Z 20200909/us-east-1/s3/aws4_request dec845474b8be721379ebb7b43a31ad34c658eaf3c9668a428fc0bc0dff02b63</StringToSign>
<SignatureProvided>cd87ad2fc7d6248f8046dbda7aa3db8914c8704d2ab4e939aeeecabf98c8ea37</SignatureProvided>
<StringToSignBytes>41 57 53 34 2d 48 4d 41 43 2d 53 48 41 32 35 36 0a 32 30 32 30 30 39 30 39 54 30 31 30 35 34 35 5a 0a 32 30 32 30 30 39 30 39 2f 75 73 2d 65 61 73 74 2d 31 2f 73 33 2f 61 77 73 34 5f 72 65 71 75 65 73 74 0a 64 65 63 38 34 35 34 37 34 62 38 62 65 37 32 31 33 37 39 65 62 62 37 62 34 33 61 33 31 61 64 33 34 63 36 35 38 65 61 66 33 63 39 36 36 38 61 34 32 38 66 63 30 62 63 30 64 66 66 30 32 62 36 33</StringToSignBytes>
<CanonicalRequest>GET somefile.png host:example.com x-amz-content-sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 x-amz-date:20200909T010545Z host;x-amz-content-sha256;x-amz-date e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855</CanonicalRequest>
<CanonicalRequestBytes>47 45 54 0a 2f 62 33 64 61 61 37 37 62 34 63 30 34 61 39 35 35 31 62 38 37 38 31 64 30 33 31 39 31 66 65 30 39 38 66 33 32 35 65 36 37 2f 73 73 2e 70 6e 67 0a 0a 68 6f 73 74 3a 62 6f 69 6e 67 2e 70 61 6e 61 63 65 61 68 65 61 6c 74 68 2e 61 69 0a 78 2d 61 6d 7a 2d 63 6f 6e 74 65 6e 74 2d 73 68 61 32 35 36 3a 65 33 62 30 63 34 34 32 39 38 66 63 31 63 31 34 39 61 66 62 66 34 63 38 39 39 36 66 62 39 32 34 32 37 61 65 34 31 65 34 36 34 39 62 39 33 34 63 61 34 39 35 39 39 31 62 37 38 35 32 62 38 35 35 0a 78 2d 61 6d 7a 2d 64 61 74 65 3a 32 30 32 30 30 39 30 39 54 30 31 30 35 34 35 5a 0a 0a 68 6f 73 74 3b 78 2d 61 6d 7a 2d 63 6f 6e 74 65 6e 74 2d 73 68 61 32 35 36 3b 78 2d 61 6d 7a 2d 64 61 74 65 0a 65 33 62 30 63 34 34 32 39 38 66 63 31 63 31 34 39 61 66 62 66 34 63 38 39 39 36 66 62 39 32 34 32 37 61 65 34 31 65 34 36 34 39 62 39 33 34 63 61 34 39 35 39 39 31 62 37 38 35 32 62 38 35 35</CanonicalRequestBytes>
<RequestId>14670F5525B7189A</RequestId>
<HostId>2xVb5KggcsomehostidpartJjLHVlD0ZDA7TIMWuThXJyYrR/B9g3+RbhPZ7xjoHzKGI=</HostId>
</Error>
Is there a way to generate pre-signed PUT URLs using CloudFront APIs? I could not find anything that is useful. Or should the client always upload using the pre-signed PUT URL generated using s3 session object?
After looking for a few days, I finally noticed the mistakes. From cloudFront control, the policy had to include both PutObject and GetObject. This allowed using the same URL with both PUT and GET methods to upload and download files respectively.
"Action": [
"s3:PutObject",
"s3:GetObject"
]
Also there seemed to be an issue when copying the *.pem file containing key, downloading the file again, and replacing the old one removed the authentication issues.

Python: Taking input from a file and storing it as an int data type

I have an input file 'input.dat':
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
I store the data in a 2D array and try to process as in the given code:
if __name__ == "__main__":
dataFile = open("/Users/atulyaratanjha/Documents/Python/P11.dat")
arr = []
for line in dataFile:
arr.append(line.split())
maxMult = 0
for i in range(16):
for j in range(20):
mult = arr[i][j] * arr[i+1][j] * arr[i+2][j] * arr[i+3][j]
maxMult = max(maxMult, mult)
for i in range(20):
for j in range(16):
mult = arr[i][j] * arr[i][j+1] * arr[i][j+2] * arr[i][j+3]
maxMult = max(maxMult, mult)
for i in range(16):
for j in range(16):
mult = arr[i][j] * arr[i+1][j+1] * arr[i+2][j+2] * arr[i+2][j+3]
maxMult = max(maxMult, mult)
print(maxMult)
It gives this Error:
TypeError: can't multiply sequence by non-int of type 'str'
I know that I can change the data into 'int' from 'str' by running a for loop, but is there any way to store the data as 'int' at the input stage?
Andrej Kesely explains you might
change the arr.append(line.split()) to arr.append(list(map(int, line.split()))) and it should work.

Enumerate prints without using print inside loop

After running this code. why are all these numbers being printed even though i haven't printed them. I think enumerate returns all these . If yes, then is there any way I can stop these.
def primeOrNot(num):
count = 0
for i in range (num-1, 1, -1):
print(i,num)
if(num%i==0):
count=1
if (count==0):
return True
else: return False
def nearest_prime(num):
nearest = 0
farthest = 0
for i in range(num+1,256):
if primeOrNot(i):
farthest = i
break
for i in range(num-1,1,-1):
if primeOrNot(i):
nearest = i
break
nearest_dist = num - nearest
farthest_dist = farthest - num
if(farthest_dist>nearest_dist):
return nearest
elif (nearest_dist>farthest_dist):
return farthest
elif(nearest_dist==farthest_dist):
return nearest
def main_code(test_case_no):
for tn in range(0,test_case_no):
str_len = input()
input_str = input()
input_list = [ord(i) for i in input_str]
for index,num in enumerate(input_list): #error here
if not primeOrNot(num):
input_list[index]=nearest_prime(num)
ouput_list = [chr(i) for i in input_list]
output_str = "".join(ouput_list)
print(output_str)
test_case_no = int(input())
main_code(test_case_no)
Input:
1
6
AFREEN
Expected output :
CGSCCO
Actual output:-
64 65
63 65
62 65
61 65
60 65
59 65
58 65
57 65
56 65
55 65
54 65
53 65
52 65
51 65
50 65
49 65
48 65
47 65
46 65
45 65
44 65
43 65
42 65
41 65
40 65
39 65
38 65
37 65
36 65
35 65
34 65
33 65
32 65
31 65
30 65
29 65
28 65
27 65
26 65
25 65
24 65
23 65
22 65
21 65
20 65
19 65
18 65
17 65
16 65
15 65
14 65
13 65
12 65
11 65
10 65
9 65
8 65
7 65
6 65
5 65
4 65
3 65
2 65
65 66
64 66
63 66
62 66
61 66
60 66
59 66
58 66
57 66
56 66
55 66
54 66
53 66
52 66
51 66
50 66
49 66
48 66
47 66
46 66
45 66
44 66
43 66
42 66
41 66
40 66
39 66
38 66
37 66
36 66
35 66
34 66
33 66
32 66
31 66
30 66
29 66
28 66
27 66
26 66
25 66
24 66
23 66
22 66
21 66
20 66
19 66
18 66
17 66
16 66
15 66
14 66
13 66
12 66
11 66
10 66
9 66
8 66
7 66
6 66
5 66
4 66
3 66
2 66
66 67
65 67
64 67
63 67
62 67
61 67
60 67
59 67
58 67
57 67
56 67
55 67
54 67
53 67
52 67
51 67
50 67
49 67
48 67
47 67
46 67
45 67
44 67
43 67
42 67
41 67
40 67
39 67
38 67
37 67
36 67
35 67
34 67
33 67
32 67
31 67
30 67
29 67
28 67
27 67
26 67
25 67
24 67
23 67
22 67
21 67
20 67
19 67
18 67
17 67
16 67
15 67
14 67
13 67
12 67
11 67
10 67
9 67
8 67
7 67
6 67
5 67
4 67
3 67
2 67
63 64
62 64
61 64
60 64
59 64
58 64
57 64
56 64
55 64
54 64
53 64
52 64
51 64
50 64
49 64
48 64
47 64
46 64
45 64
44 64
43 64
42 64
41 64
40 64
39 64
38 64
37 64
36 64
35 64
34 64
33 64
32 64
31 64
30 64
29 64
28 64
27 64
26 64
25 64
24 64
23 64
22 64
21 64
20 64
19 64
18 64
17 64
16 64
15 64
14 64
13 64
12 64
11 64
10 64
9 64
8 64
7 64
6 64
5 64
4 64
3 64
2 64
62 63
61 63
60 63
59 63
58 63
57 63
56 63
55 63
54 63
53 63
52 63
51 63
50 63
49 63
48 63
47 63
46 63
45 63
44 63
43 63
42 63
41 63
40 63
39 63
38 63
37 63
36 63
35 63
34 63
33 63
32 63
31 63
30 63
29 63
28 63
27 63
26 63
25 63
24 63
23 63
22 63
21 63
20 63
19 63
18 63
17 63
16 63
15 63
14 63
13 63
12 63
11 63
10 63
9 63
8 63
7 63
6 63
5 63
4 63
3 63
2 63
61 62
60 62
59 62
58 62
57 62
56 62
55 62
54 62
53 62
52 62
51 62
50 62
49 62
48 62
47 62
46 62
45 62
44 62
43 62
42 62
41 62
40 62
39 62
38 62
37 62
36 62
35 62
34 62
33 62
32 62
31 62
30 62
29 62
28 62
27 62
26 62
25 62
24 62
23 62
22 62
21 62
20 62
19 62
18 62
17 62
16 62
15 62
14 62
13 62
12 62
11 62
10 62
9 62
8 62
7 62
6 62
5 62
4 62
3 62
2 62
60 61
59 61
58 61
57 61
56 61
55 61
54 61
53 61
52 61
51 61
50 61
49 61
48 61
47 61
46 61
45 61
44 61
43 61
42 61
41 61
40 61
39 61
38 61
37 61
36 61
35 61
34 61
33 61
32 61
31 61
30 61
29 61
28 61
27 61
26 61
25 61
24 61
23 61
22 61
21 61
20 61
19 61
18 61
17 61
16 61
15 61
14 61
13 61
12 61
11 61
10 61
9 61
8 61
7 61
6 61
5 61
4 61
3 61
2 61
69 70
68 70
67 70
66 70
65 70
64 70
63 70
62 70
61 70
60 70
59 70
58 70
57 70
56 70
55 70
54 70
53 70
52 70
51 70
50 70
49 70
48 70
47 70
46 70
45 70
44 70
43 70
42 70
41 70
40 70
39 70
38 70
37 70
36 70
35 70
34 70
33 70
32 70
31 70
30 70
29 70
28 70
27 70
26 70
25 70
24 70
23 70
22 70
21 70
20 70
19 70
18 70
17 70
16 70
15 70
14 70
13 70
12 70
11 70
10 70
9 70
8 70
7 70
6 70
5 70
4 70
3 70
2 70
70 71
69 71
68 71
67 71
66 71
65 71
64 71
63 71
62 71
61 71
60 71
59 71
58 71
57 71
56 71
55 71
54 71
53 71
52 71
51 71
50 71
49 71
48 71
47 71
46 71
45 71
44 71
43 71
42 71
41 71
40 71
39 71
38 71
37 71
36 71
35 71
34 71
33 71
32 71
31 71
30 71
29 71
28 71
27 71
26 71
25 71
24 71
23 71
22 71
21 71
20 71
19 71
18 71
17 71
16 71
15 71
14 71
13 71
12 71
11 71
10 71
9 71
8 71
7 71
6 71
5 71
4 71
3 71
2 71
68 69
67 69
66 69
65 69
64 69
63 69
62 69
61 69
60 69
59 69
58 69
57 69
56 69
55 69
54 69
53 69
52 69
51 69
50 69
49 69
48 69
47 69
46 69
45 69
44 69
43 69
42 69
41 69
40 69
39 69
38 69
37 69
36 69
35 69
34 69
33 69
32 69
31 69
30 69
29 69
28 69
27 69
26 69
25 69
24 69
23 69
22 69
21 69
20 69
19 69
18 69
17 69
16 69
15 69
14 69
13 69
12 69
11 69
10 69
9 69
8 69
7 69
6 69
5 69
4 69
3 69
2 69
67 68
66 68
65 68
64 68
63 68
62 68
61 68
60 68
59 68
58 68
57 68
56 68
55 68
54 68
53 68
52 68
51 68
50 68
49 68
48 68
47 68
46 68
45 68
44 68
43 68
42 68
41 68
40 68
39 68
38 68
37 68
36 68
35 68
34 68
33 68
32 68
31 68
30 68
29 68
28 68
27 68
26 68
25 68
24 68
23 68
22 68
21 68
20 68
19 68
18 68
17 68
16 68
15 68
14 68
13 68
12 68
11 68
10 68
9 68
8 68
7 68
6 68
5 68
4 68
3 68
2 68
66 67
65 67
64 67
63 67
62 67
61 67
60 67
59 67
58 67
57 67
56 67
55 67
54 67
53 67
52 67
51 67
50 67
49 67
48 67
47 67
46 67
45 67
44 67
43 67
42 67
41 67
40 67
39 67
38 67
37 67
36 67
35 67
34 67
33 67
32 67
31 67
30 67
29 67
28 67
27 67
26 67
25 67
24 67
23 67
22 67
21 67
20 67
19 67
18 67
17 67
16 67
15 67
14 67
13 67
12 67
11 67
10 67
9 67
8 67
7 67
6 67
5 67
4 67
3 67
2 67
81 82
80 82
79 82
78 82
77 82
76 82
75 82
74 82
73 82
72 82
71 82
70 82
69 82
68 82
67 82
66 82
65 82
64 82
63 82
62 82
61 82
60 82
59 82
58 82
57 82
56 82
55 82
54 82
53 82
52 82
51 82
50 82
49 82
48 82
47 82
46 82
45 82
44 82
43 82
42 82
41 82
40 82
39 82
38 82
37 82
36 82
35 82
34 82
33 82
32 82
31 82
30 82
29 82
28 82
27 82
26 82
25 82
24 82
23 82
22 82
21 82
20 82
19 82
18 82
17 82
16 82
15 82
14 82
13 82
12 82
11 82
10 82
9 82
8 82
7 82
6 82
5 82
4 82
3 82
2 82
82 83
81 83
80 83
79 83
78 83
77 83
76 83
75 83
74 83
73 83
72 83
71 83
70 83
69 83
68 83
67 83
66 83
65 83
64 83
63 83
62 83
61 83
60 83
59 83
58 83
57 83
56 83
55 83
54 83
53 83
52 83
51 83
50 83
49 83
48 83
47 83
46 83
45 83
44 83
43 83
42 83
41 83
40 83
39 83
38 83
37 83
36 83
35 83
34 83
33 83
32 83
31 83
30 83
29 83
28 83
27 83
26 83
25 83
24 83
23 83
22 83
21 83
20 83
19 83
18 83
17 83
16 83
15 83
14 83
13 83
12 83
11 83
10 83
9 83
8 83
7 83
6 83
5 83
4 83
3 83
2 83
80 81
79 81
78 81
77 81
76 81
75 81
74 81
73 81
72 81
71 81
70 81
69 81
68 81
67 81
66 81
65 81
64 81
63 81
62 81
61 81
60 81
59 81
58 81
57 81
56 81
55 81
54 81
53 81
52 81
51 81
50 81
49 81
48 81
47 81
46 81
45 81
44 81
43 81
42 81
41 81
40 81
39 81
38 81
37 81
36 81
35 81
34 81
33 81
32 81
31 81
30 81
29 81
28 81
27 81
26 81
25 81
24 81
23 81
22 81
21 81
20 81
19 81
18 81
17 81
16 81
15 81
14 81
13 81
12 81
11 81
10 81
9 81
8 81
7 81
6 81
5 81
4 81
3 81
2 81
79 80
78 80
77 80
76 80
75 80
74 80
73 80
72 80
71 80
70 80
69 80
68 80
67 80
66 80
65 80
64 80
63 80
62 80
61 80
60 80
59 80
58 80
57 80
56 80
55 80
54 80
53 80
52 80
51 80
50 80
49 80
48 80
47 80
46 80
45 80
44 80
43 80
42 80
41 80
40 80
39 80
38 80
37 80
36 80
35 80
34 80
33 80
32 80
31 80
30 80
29 80
28 80
27 80
26 80
25 80
24 80
23 80
22 80
21 80
20 80
19 80
18 80
17 80
16 80
15 80
14 80
13 80
12 80
11 80
10 80
9 80
8 80
7 80
6 80
5 80
4 80
3 80
2 80
78 79
77 79
76 79
75 79
74 79
73 79
72 79
71 79
70 79
69 79
68 79
67 79
66 79
65 79
64 79
63 79
62 79
61 79
60 79
59 79
58 79
57 79
56 79
55 79
54 79
53 79
52 79
51 79
50 79
49 79
48 79
47 79
46 79
45 79
44 79
43 79
42 79
41 79
40 79
39 79
38 79
37 79
36 79
35 79
34 79
33 79
32 79
31 79
30 79
29 79
28 79
27 79
26 79
25 79
24 79
23 79
22 79
21 79
20 79
19 79
18 79
17 79
16 79
15 79
14 79
13 79
12 79
11 79
10 79
9 79
8 79
7 79
6 79
5 79
4 79
3 79
2 79
68 69
67 69
66 69
65 69
64 69
63 69
62 69
61 69
60 69
59 69
58 69
57 69
56 69
55 69
54 69
53 69
52 69
51 69
50 69
49 69
48 69
47 69
46 69
45 69
44 69
43 69
42 69
41 69
40 69
39 69
38 69
37 69
36 69
35 69
34 69
33 69
32 69
31 69
30 69
29 69
28 69
27 69
26 69
25 69
24 69
23 69
22 69
21 69
20 69
19 69
18 69
17 69
16 69
15 69
14 69
13 69
12 69
11 69
10 69
9 69
8 69
7 69
6 69
5 69
4 69
3 69
2 69
69 70
68 70
67 70
66 70
65 70
64 70
63 70
62 70
61 70
60 70
59 70
58 70
57 70
56 70
55 70
54 70
53 70
52 70
51 70
50 70
49 70
48 70
47 70
46 70
45 70
44 70
43 70
42 70
41 70
40 70
39 70
38 70
37 70
36 70
35 70
34 70
33 70
32 70
31 70
30 70
29 70
28 70
27 70
26 70
25 70
24 70
23 70
22 70
21 70
20 70
19 70
18 70
17 70
16 70
15 70
14 70
13 70
12 70
11 70
10 70
9 70
8 70
7 70
6 70
5 70
4 70
3 70
2 70
70 71
69 71
68 71
67 71
66 71
65 71
64 71
63 71
62 71
61 71
60 71
59 71
58 71
57 71
56 71
55 71
54 71
53 71
52 71
51 71
50 71
49 71
48 71
47 71
46 71
45 71
44 71
43 71
42 71
41 71
40 71
39 71
38 71
37 71
36 71
35 71
34 71
33 71
32 71
31 71
30 71
29 71
28 71
27 71
26 71
25 71
24 71
23 71
22 71
21 71
20 71
19 71
18 71
17 71
16 71
15 71
14 71
13 71
12 71
11 71
10 71
9 71
8 71
7 71
6 71
5 71
4 71
3 71
2 71
67 68
66 68
65 68
64 68
63 68
62 68
61 68
60 68
59 68
58 68
57 68
56 68
55 68
54 68
53 68
52 68
51 68
50 68
49 68
48 68
47 68
46 68
45 68
44 68
43 68
42 68
41 68
40 68
39 68
38 68
37 68
36 68
35 68
34 68
33 68
32 68
31 68
30 68
29 68
28 68
27 68
26 68
25 68
24 68
23 68
22 68
21 68
20 68
19 68
18 68
17 68
16 68
15 68
14 68
13 68
12 68
11 68
10 68
9 68
8 68
7 68
6 68
5 68
4 68
3 68
2 68
66 67
65 67
64 67
63 67
62 67
61 67
60 67
59 67
58 67
57 67
56 67
55 67
54 67
53 67
52 67
51 67
50 67
49 67
48 67
47 67
46 67
45 67
44 67
43 67
42 67
41 67
40 67
39 67
38 67
37 67
36 67
35 67
34 67
33 67
32 67
31 67
30 67
29 67
28 67
27 67
26 67
25 67
24 67
23 67
22 67
21 67
20 67
19 67
18 67
17 67
16 67
15 67
14 67
13 67
12 67
11 67
10 67
9 67
8 67
7 67
6 67
5 67
4 67
3 67
2 67
68 69
67 69
66 69
65 69
64 69
63 69
62 69
61 69
60 69
59 69
58 69
57 69
56 69
55 69
54 69
53 69
52 69
51 69
50 69
49 69
48 69
47 69
46 69
45 69
44 69
43 69
42 69
41 69
40 69
39 69
38 69
37 69
36 69
35 69
34 69
33 69
32 69
31 69
30 69
29 69
28 69
27 69
26 69
25 69
24 69
23 69
22 69
21 69
20 69
19 69
18 69
17 69
16 69
15 69
14 69
13 69
12 69
11 69
10 69
9 69
8 69
7 69
6 69
5 69
4 69
3 69
2 69
69 70
68 70
67 70
66 70
65 70
64 70
63 70
62 70
61 70
60 70
59 70
58 70
57 70
56 70
55 70
54 70
53 70
52 70
51 70
50 70
49 70
48 70
47 70
46 70
45 70
44 70
43 70
42 70
41 70
40 70
39 70
38 70
37 70
36 70
35 70
34 70
33 70
32 70
31 70
30 70
29 70
28 70
27 70
26 70
25 70
24 70
23 70
22 70
21 70
20 70
19 70
18 70
17 70
16 70
15 70
14 70
13 70
12 70
11 70
10 70
9 70
8 70
7 70
6 70
5 70
4 70
3 70
2 70
70 71
69 71
68 71
67 71
66 71
65 71
64 71
63 71
62 71
61 71
60 71
59 71
58 71
57 71
56 71
55 71
54 71
53 71
52 71
51 71
50 71
49 71
48 71
47 71
46 71
45 71
44 71
43 71
42 71
41 71
40 71
39 71
38 71
37 71
36 71
35 71
34 71
33 71
32 71
31 71
30 71
29 71
28 71
27 71
26 71
25 71
24 71
23 71
22 71
21 71
20 71
19 71
18 71
17 71
16 71
15 71
14 71
13 71
12 71
11 71
10 71
9 71
8 71
7 71
6 71
5 71
4 71
3 71
2 71
67 68
66 68
65 68
64 68
63 68
62 68
61 68
60 68
59 68
58 68
57 68
56 68
55 68
54 68
53 68
52 68
51 68
50 68
49 68
48 68
47 68
46 68
45 68
44 68
43 68
42 68
41 68
40 68
39 68
38 68
37 68
36 68
35 68
34 68
33 68
32 68
31 68
30 68
29 68
28 68
27 68
26 68
25 68
24 68
23 68
22 68
21 68
20 68
19 68
18 68
17 68
16 68
15 68
14 68
13 68
12 68
11 68
10 68
9 68
8 68
7 68
6 68
5 68
4 68
3 68
2 68
66 67
65 67
64 67
63 67
62 67
61 67
60 67
59 67
58 67
57 67
56 67
55 67
54 67
53 67
52 67
51 67
50 67
49 67
48 67
47 67
46 67
45 67
44 67
43 67
42 67
41 67
40 67
39 67
38 67
37 67
36 67
35 67
34 67
33 67
32 67
31 67
30 67
29 67
28 67
27 67
26 67
25 67
24 67
23 67
22 67
21 67
20 67
19 67
18 67
17 67
16 67
15 67
14 67
13 67
12 67
11 67
10 67
9 67
8 67
7 67
6 67
5 67
4 67
3 67
2 67
77 78
76 78
75 78
74 78
73 78
72 78
71 78
70 78
69 78
68 78
67 78
66 78
65 78
64 78
63 78
62 78
61 78
60 78
59 78
58 78
57 78
56 78
55 78
54 78
53 78
52 78
51 78
50 78
49 78
48 78
47 78
46 78
45 78
44 78
43 78
42 78
41 78
40 78
39 78
38 78
37 78
36 78
35 78
34 78
33 78
32 78
31 78
30 78
29 78
28 78
27 78
26 78
25 78
24 78
23 78
22 78
21 78
20 78
19 78
18 78
17 78
16 78
15 78
14 78
13 78
12 78
11 78
10 78
9 78
8 78
7 78
6 78
5 78
4 78
3 78
2 78
78 79
77 79
76 79
75 79
74 79
73 79
72 79
71 79
70 79
69 79
68 79
67 79
66 79
65 79
64 79
63 79
62 79
61 79
60 79
59 79
58 79
57 79
56 79
55 79
54 79
53 79
52 79
51 79
50 79
49 79
48 79
47 79
46 79
45 79
44 79
43 79
42 79
41 79
40 79
39 79
38 79
37 79
36 79
35 79
34 79
33 79
32 79
31 79
30 79
29 79
28 79
27 79
26 79
25 79
24 79
23 79
22 79
21 79
20 79
19 79
18 79
17 79
16 79
15 79
14 79
13 79
12 79
11 79
10 79
9 79
8 79
7 79
6 79
5 79
4 79
3 79
2 79
76 77
75 77
74 77
73 77
72 77
71 77
70 77
69 77
68 77
67 77
66 77
65 77
64 77
63 77
62 77
61 77
60 77
59 77
58 77
57 77
56 77
55 77
54 77
53 77
52 77
51 77
50 77
49 77
48 77
47 77
46 77
45 77
44 77
43 77
42 77
41 77
40 77
39 77
38 77
37 77
36 77
35 77
34 77
33 77
32 77
31 77
30 77
29 77
28 77
27 77
26 77
25 77
24 77
23 77
22 77
21 77
20 77
19 77
18 77
17 77
16 77
15 77
14 77
13 77
12 77
11 77
10 77
9 77
8 77
7 77
6 77
5 77
4 77
3 77
2 77
75 76
74 76
73 76
72 76
71 76
70 76
69 76
68 76
67 76
66 76
65 76
64 76
63 76
62 76
61 76
60 76
59 76
58 76
57 76
56 76
55 76
54 76
53 76
52 76
51 76
50 76
49 76
48 76
47 76
46 76
45 76
44 76
43 76
42 76
41 76
40 76
39 76
38 76
37 76
36 76
35 76
34 76
33 76
32 76
31 76
30 76
29 76
28 76
27 76
26 76
25 76
24 76
23 76
22 76
21 76
20 76
19 76
18 76
17 76
16 76
15 76
14 76
13 76
12 76
11 76
10 76
9 76
8 76
7 76
6 76
5 76
4 76
3 76
2 76
74 75
73 75
72 75
71 75
70 75
69 75
68 75
67 75
66 75
65 75
64 75
63 75
62 75
61 75
60 75
59 75
58 75
57 75
56 75
55 75
54 75
53 75
52 75
51 75
50 75
49 75
48 75
47 75
46 75
45 75
44 75
43 75
42 75
41 75
40 75
39 75
38 75
37 75
36 75
35 75
34 75
33 75
32 75
31 75
30 75
29 75
28 75
27 75
26 75
25 75
24 75
23 75
22 75
21 75
20 75
19 75
18 75
17 75
16 75
15 75
14 75
13 75
12 75
11 75
10 75
9 75
8 75
7 75
6 75
5 75
4 75
3 75
2 75
73 74
72 74
71 74
70 74
69 74
68 74
67 74
66 74
65 74
64 74
63 74
62 74
61 74
60 74
59 74
58 74
57 74
56 74
55 74
54 74
53 74
52 74
51 74
50 74
49 74
48 74
47 74
46 74
45 74
44 74
43 74
42 74
41 74
40 74
39 74
38 74
37 74
36 74
35 74
34 74
33 74
32 74
31 74
30 74
29 74
28 74
27 74
26 74
25 74
24 74
23 74
22 74
21 74
20 74
19 74
18 74
17 74
16 74
15 74
14 74
13 74
12 74
11 74
10 74
9 74
8 74
7 74
6 74
5 74
4 74
3 74
2 74
72 73
71 73
70 73
69 73
68 73
67 73
66 73
65 73
64 73
63 73
62 73
61 73
60 73
59 73
58 73
57 73
56 73
55 73
54 73
53 73
52 73
51 73
50 73
49 73
48 73
47 73
46 73
45 73
44 73
43 73
42 73
41 73
40 73
39 73
38 73
37 73
36 73
35 73
34 73
33 73
32 73
31 73
30 73
29 73
28 73
27 73
26 73
25 73
24 73
23 73
22 73
21 73
20 73
19 73
18 73
17 73
16 73
15 73
14 73
13 73
12 73
11 73
10 73
9 73
8 73
7 73
6 73
5 73
4 73
3 73
2 73
CGSCCO
Note: Every pair of numbers are in new line in actual output.

Can I format n numbers in Python

How can I print all numbers in a given range to given number of columns, where every colums is of width 6 character and there is a space between colums? I tried to use format:
for i in range(0,nolines):
for j in range(0,nocolums):
print("{0:6}{1:6}".format(number1,number2))
but found that this approach won't work as I need more general code to format n, where n is given by user input, numbers instead of two. So can I print n numbers by using format?
For example, if input is
min = 20, max = 104, numbers on one line = 10
the program should print
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
100 101 102 103 104
def print_range(start, stop, ncolumns, width=6):
for i in range(start, stop, ncolumns):
print(' '.join(['{:{}d}'.format(j, width)
for j in range(i, min(i + ncolumns, stop))]))
Example:
>>> print_range(20, 105, ncolumns=10)
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
100 101 102 103 104
You could use the str.rjust method:
lines = [
[1, 2, 3],
[111, 222, 333],
]
for line in lines:
for n in line:
print(str(n).rjust(6), end='')
print()

Resources