Understanding the Format of Values in the Python Array

Asked 2 years ago, Updated 2 years ago, 49 views

I think it's very simple, but I'm stuck in a problem.

I am currently using python (version 3.6.0) to create an array.
You just want to insert the values that you read from the text file into the array.

Specifically,
Suppose you have sample1.txt and sample2.txt.

where
sample1.txt is located
20035.87109375,23184.52539062
20031.375,23162.98046875

contains the value and

sample2.txt contains the following:
20037.72460938,23142.20117188
20051.09765625,23124.33203125

Think of as a value.

I would like to load this file and eventually create an array using the following format:

[
 [[ 20035.87109375  23184.52539062]
  [ 20031.375       23162.98046875]]

 [[ 20037.72460938  23142.20117188]
  [ 20051.09765625  23124.33203125]]
]

We know to insert text file values into the array.

If you insert it as a number, click
[20035.87109375, 23184.52539062]

will look like .

If you insert it as a string, click
['20035.87109375 23184.52539062']

It will look like .
I would like to insert the problem with a format that is neither numeric nor string (see the example above).

If anyone knows what format it is and how to do it, please let me know.

Thank you for your cooperation.

python

2022-09-30 21:24

2 Answers

To see the output, we assume that you should use numpy.array.

[
 [[ 20035.87109375  23184.52539062]
  [ 20031.375       23162.98046875]
 ]

 [[ 20037.72460938  23142.20117188]
  [ 20051.09765625  23124.33203125]]
]

If you enter the following command in the interaction shell,

>>import numpy as np
>>>data=np.array([10,20],[30,40]])
>> print data # print(data) if python3

You will get similar output.

 [[1020]
 [30 40]]

[Reference]
· numpy.array — NumPy v1.12 Manual (https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html)


2022-09-30 21:24

It seems that you just want to add a blank space after the data to make it a string with the starting position (the number of characters from the beginning) of each data.

Take the data in the first row of Sample1 as a sample.

#Actually, the code reads one line from Sample1.txt, but uses substitution instead
firstLineOfSample1="20035.87109375,23184.52539062"
Split the string with split=firstLineOfSample1.split(", ")#", "
# In this example, split[0] = "20035.87109375", split[1] = "23184.52539062", and so on.
Split[0] = split[0]+"# Add a blank character to the end.(Added 16-character spaces so that the first 16 characters can be truncated)
formatted = split[0].[:16] + split[1]# Make the first item a 16-character string, including a delimited space, followed by the second item (where .[:16] means extracting the first 16 characters).
# As a result, the formatted contains the string "20035.8710937523184.52539062"

In this way, a string containing two numbers is obtained, so you can insert it into the array.


2022-09-30 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.