[英]Read arrays from file
I need to read points from 2 files (.txt or.xls) as an array, and combine them to run an algorithm that fits these points into an ellipse.我需要从 2 个文件(.txt 或 .xls)中读取点作为数组,并将它们组合起来以运行将这些点拟合成椭圆的算法。
From the first file of 5 columns I have to read the arrays X's of 5 elements:从 5 列的第一个文件中,我必须读取 5 个元素的 arrays X:
3586,02124 2391,50342 837,45227 -837,29681 -2385,9751
3587,69238 2387,48218 836,604 -840,75067 -2390,17529
3588,44531 2387,44556 836,00555 -840,79022 -2389,77612
3588,08203 2388,25439 836,26544 -840,17017 -2389,07544
3587,66553 2389,05566 836,53046 -839,53912 -2388,40405
3587,26636 2389,86255 836,79382 -838,91455 -2387,7168
........
From the second file of 5 columns I have to read the arrays Y's of 5 elements:从 5 列的第二个文件中,我必须读取 5 个元素的 arrays Y:
843,19598 2396,10278 3579,1377 4210,15674 4209,37549
841,9397 2397,21948 3573,11963 4205,89209 4226,73926
842,01642 2397,72266 3573,06494 4202,88379 4226,93799
842,22083 2397,47974 3574,27515 4204,19043 4223,8208
842,42065 2397,20142 3575,47437 4205,52246 4220,64795
......
Then I must create the variable pot:然后我必须创建变量 pot:
pot = (x, y)
Which is the input data for calculation.这是计算的输入数据。 The first line of the first file
(x(1))
corresponds to the first line of the second file (y(1))
.第一个文件的第一行
(x(1))
对应于第二个文件的第一行(y(1))
。 The algorithm takes x and y from these five points (3586,02124;843,19598), (2391,50342; 2396,10278),....
and fits the ellipse.该算法从这五个点
(3586,02124;843,19598), (2391,50342; 2396,10278),....
获取 x 和 y,并拟合椭圆。 So for each sets of point (or line) I will have an ellipse.所以对于每组点(或线)我都会有一个椭圆。
To sum it up, I am looking for a code to read these points and also which loop will be suitable to run the algorithm for each set of points.总而言之,我正在寻找一个代码来读取这些点,以及哪个循环适合为每组点运行算法。
Any ideas?有任何想法吗?
Don't exactly know what you mean but:不完全明白你的意思,但是:
array = []
with open('test.txt', 'r') as file:
for line in file:
print(line.strip('\n').split(' '))
gives me this output:给我这个 output:
['3586,02124', '2391,50342', '837,45227', ' -837,29681', '-2385,9751']
['3587,69238', '2387,48218', '836,604', '', ' -840,75067', '-2390,17529']
['3588,44531', '2387,44556', '836,00555', ' -840,79022', '-2389,77612']
['3588,08203', '2388,25439', '836,26544', ' -840,17017', '-2389,07544']
['3587,66553', '2389,05566', '836,53046', ' -839,53912', '-2388,40405']
['3587,26636', '2389,86255', '836,79382', ' -838,91455', '-2387,7168']
I think you'll be able to make it further.我想你可以更进一步。 You might want to do another strip but that's your file that has an inconsistent spacing before the 4th column
您可能想再做一条,但那是您的文件在第 4 列之前的间距不一致
import pandas as pd
# Files to read
files = ['f1.csv', 'f2.csv']
# Read each file and create a dataframe
# df[0] contains a dataframe for f1
# df[1] contains a dataframe for f2
df = []
for file in files:
df.append(pd.read_csv(file, sep='\\s+', header=None))
print(f'Dataframe of f1\n{df[0]}\n')
print(f'Dataframe of f2\n{df[1]}\n')
# Get the number of rows from df[0]
# df[1] must be of the same dimension (rows x columns)
nrows = df[0].shape[0]
ncols = df[0].shape[1]
for row in range(nrows):
print(f'\nrow {row}')
for col in range(ncols):
print(f'col {col} of f1 and f2')
col_f1 = df[0].iloc[row, col].split(',')
col_f2 = df[1].iloc[row, col].split(',')
print(f'{col_f1}, {col_f2}')
# Here you have (3586,02124;843,19598)....
# In this format: col_f1, col_f2 = [3586, 02124], [843, 19598]...
# Don't forget to convert to float() the numbers
# Ex. pt_x = float(col_f1[0])
Dataframe of f1
0 1 2 3 4
0 3586,02124 2391,50342 837,45227 -837,29681 -2385,9751
1 3587,69238 2387,48218 836,604 -840,75067 -2390,17529
2 3588,44531 2387,44556 836,00555 -840,79022 -2389,77612
3 3588,08203 2388,25439 836,26544 -840,17017 -2389,07544
4 3587,66553 2389,05566 836,53046 -839,53912 -2388,40405
Dataframe of f2
0 1 2 3 4
0 843,19598 2396,10278 3579,1377 4210,15674 4209,37549
1 841,9397 2397,21948 3573,11963 4205,89209 4226,73926
2 842,01642 2397,72266 3573,06494 4202,88379 4226,93799
3 842,22083 2397,47974 3574,27515 4204,19043 4223,8208
4 842,42065 2397,20142 3575,47437 4205,52246 4220,64795
row 0
col 0 of f1 and f2
['3586', '02124'], ['843', '19598']
col 1 of f1 and f2
['2391', '50342'], ['2396', '10278']
col 2 of f1 and f2
['837', '45227'], ['3579', '1377']
col 3 of f1 and f2
['-837', '29681'], ['4210', '15674']
col 4 of f1 and f2
['-2385', '9751'], ['4209', '37549']
row 1
col 0 of f1 and f2
['3587', '69238'], ['841', '9397']
col 1 of f1 and f2
['2387', '48218'], ['2397', '21948']
col 2 of f1 and f2
['836', '604'], ['3573', '11963']
col 3 of f1 and f2
['-840', '75067'], ['4205', '89209']
col 4 of f1 and f2
['-2390', '17529'], ['4226', '73926']
row 2
col 0 of f1 and f2
['3588', '44531'], ['842', '01642']
col 1 of f1 and f2
['2387', '44556'], ['2397', '72266']
col 2 of f1 and f2
['836', '00555'], ['3573', '06494']
col 3 of f1 and f2
['-840', '79022'], ['4202', '88379']
col 4 of f1 and f2
['-2389', '77612'], ['4226', '93799']
row 3
col 0 of f1 and f2
['3588', '08203'], ['842', '22083']
col 1 of f1 and f2
['2388', '25439'], ['2397', '47974']
col 2 of f1 and f2
['836', '26544'], ['3574', '27515']
col 3 of f1 and f2
['-840', '17017'], ['4204', '19043']
col 4 of f1 and f2
['-2389', '07544'], ['4223', '8208']
row 4
col 0 of f1 and f2
['3587', '66553'], ['842', '42065']
col 1 of f1 and f2
['2389', '05566'], ['2397', '20142']
col 2 of f1 and f2
['836', '53046'], ['3575', '47437']
col 3 of f1 and f2
['-839', '53912'], ['4205', '52246']
col 4 of f1 and f2
['-2388', '40405'], ['4220', '64795']
You can continue from there, now (x,y) pairs are easily accessible.您可以从那里继续,现在 (x,y) 对很容易访问。 I don't know what do you want to do with pot = (x,y) so I can't help further.
我不知道你想用 pot = (x,y) 做什么,所以我无法提供进一步的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.