Python 如何从文件中进行计算?
我有一个包含两列的文件,需要对它们应用这个等式,就像Python 如何从文件中进行计算?,python,Python,我有一个包含两列的文件,需要对它们应用这个等式,就像 x y 1.2 6.8 2.5 7.0 3 8 4 9 5 10 方程式是 de = sqrt((xi-xj)^2-(yi-yj)^2) 这意味着结果将是一列 row1 = sqrt((x1-x2)^2-(y1-y2)^2) row2 = sqrt((x1-x3)^2-(y1-y3)^2) 对每个点x1和其他点做这个方程,对其他点做y1,直到完成,然后开始计算 row 6 = sqrt((x2-x3)
x y
1.2 6.8
2.5 7.0
3 8
4 9
5 10
方程式是
de = sqrt((xi-xj)^2-(yi-yj)^2)
这意味着结果将是一列
row1 = sqrt((x1-x2)^2-(y1-y2)^2)
row2 = sqrt((x1-x3)^2-(y1-y3)^2)
对每个点x1和其他点做这个方程,对其他点做y1,直到完成,然后开始计算
row 6 = sqrt((x2-x3)^2-(y2-y3)^2)
row 7 = sqrt((x2-x4)^2-(y2-y4)^2)
对每个点做这个等式,x2到其他点,y2到其他点,直到完成,依此类推,直到完成所有的x和y,并将结果存储在一个文件中
我尝试使用2个数组来实现这一点,并将数字存储在其中,然后进行计算,但数据太大,数组将是错误的选择。。如何在python中实现这一点。。从文件中读取每个值的I和j
我的努力,如果太糟糕,我会感到抱歉
import math
with open('columnss.txt', 'r', encoding='utf-8') as f:
for line in f:
[x, y] = (int(n) for n in line.split())
d = math.sqrt(((x[0] - y[0])**2) + ((x[1] - y[1])** 2))
with open('result.txt', 'w', encoding='utf-8') as f1:
f1.write( str(d) + '\n')
我得到
我在excel中进行了计算,但也尝试使用python进行计算
我应该把每一列放在一个单独的文件中,以便更容易捕捉数字,还是可以用同一个文件来实现这一点
*您需要在输入文件中循环两次。第二个循环可以跳过第一个循环中该行之前的所有行 如果可以将文件内容加载到列表或数组中,则可以通过迭代索引而不是跳过行来更轻松地完成此操作 此外,您应该只打开输出文件一次。每次通过循环时都会覆盖它
import cmath
with open('columnss.txt', 'r', encoding='utf-8') as f1, open('columnss.txt', 'r', encoding='utf-8') as f2, open('result.txt', 'w', encoding='utf-8') as outfile:
for i1, line in enumerate(f1):
x1, y1 = (float(n) for n in line.split())
f2.seek(0)
for i2, line in enumerate(f2):
if i1 < i2:
x2, y2 = (float(n) for n in line.split())
print(cmath.sqrt((x1-x2)**2-(y1-y2)**2), file=outfile)
导入cmath
将open('columnss.txt','r',encoding='utf-8')作为f1,将open('columnss.txt','r',encoding='utf-8')作为f2,将open('result.txt','w',encoding='utf-8')作为输出文件:
对于i1,枚举(f1)中的行:
x1,y1=(在line.split()中n的浮点(n)
f2.寻道(0)
对于i2,枚举(f2)中的行:
如果i1
试试这个:
import math
with open('result.txt', 'w', encoding='utf-8') as f1:
with open('columnss.txt', 'r', encoding='utf-8') as f:
while True :
line=f.readline()
[x, y] = (int(float(n)) for n in line.split())
if ((x[0] - y[0])**2) + ((x[1] - y[1])** 2)> 0:
d = math.sqrt(((x[0] - y[0])**2) + ((x[1] - y[1])** 2))
f1.write(line +':' str(d) + '\n')
if not line :
break
f.close()
f.close()
每当有一个问题看起来像是可以在excel工作表中完成的,并且想要启用python方法来完成它时,我就使用pandas 我想你也可以使用熊猫 下面是读取“columns.txt”文件并输出为“output.csv”的代码
import pandas as pd
import cmath
df = pd.read_csv('columns.txt', sep=r"\s+") # read columns.txt into a dataframe, using space as deliimter
df.dropna(inplace=True,axis=1) # multiple whitespaces create NA columns. Better to use csv file
df = df.astype(float) # specify the columsn as type float
print("-"*20 + "Input" + "-"*20)
print(df) #
print("-"*50)
for index, row in df.iterrows():
origin=row # specify current row as origin
'''
Adding equation column
Here we are using a lambda function (same as de used in the question)
and creating a new column called equation
'''
df["equation from row {}".format(index)]=df.apply(lambda row_lambda: cmath.sqrt((origin.x-row_lambda.x)**2 - (origin.y-row_lambda.y)**2), axis=1)
print("-"*20 + "Output" + "-"*20)
print(df)
print("-"*50)
# Save this output as csv file (even excel is possible)
df.to_csv('Output.csv')```
The output will look like:
--------------------Input--------------------
x y
0 -99.9580 -28.84930
1 -71.5378 -26.77280
2 -91.6913 -40.90390
3 -69.0989 -12.95010
4 -79.6443 -9.20575
5 -92.1975 -20.02760
6 -99.7732 -14.26070
7 -80.3767 -18.16040
--------------------------------------------------
--------------------Output--------------------
x y distance from row 0 distance from row 1 \
0 -99.9580 -28.84930 0j (28.344239552155912+0j)
1 -71.5378 -26.77280 (28.344239552155912+0j) 0j
2 -91.6913 -40.90390 8.773542743384796j (14.369257985017867+0j)
3 -69.0989 -12.95010 (26.448052710360358+0j) 13.605837059144871j
4 -79.6443 -9.20575 (5.174683670283624+0j) 15.584797189970107j
5 -92.1975 -20.02760 4.194881481043308j (19.527556965734348+0j)
6 -99.7732 -14.26070 14.587429482948666j (25.31175945583396+0j)
7 -80.3767 -18.16040 (16.40654523292457+0j) (1.9881447256173002+0j)
distance from row 2 distance from row 3 distance from row 4 \
0 8.773542743384796j (26.448052710360358+0j) (5.174683670283624+0j)
1 (14.369257985017867+0j) 13.605837059144871j -15.584797189970107j
2 0j 16.462028935705348j 29.319660714655278j
3 16.462028935705348j 0j (9.858260710566546-0j)
4 29.319660714655278j (9.858260710566546+0j) 0j
5 20.87016203219323j (21.987594586720945+0j) (6.361634445447185+0j)
6 25.387851398454337j (30.646288651809048+0j) (19.483841913429192+0j)
7 19.72933397482034j (10.002077121778257+0j) 8.924648276682952j
distance from row 5 distance from row 6 distance from row 7
0 4.194881481043308j 14.587429482948666j (16.40654523292457+0j)
1 (19.527556965734348-0j) (25.31175945583396-0j) (1.9881447256173002-0j)
2 -20.87016203219323j -25.387851398454337j 19.72933397482034j
3 (21.987594586720945+0j) (30.646288651809048+0j) (10.002077121778257+0j)
4 (6.361634445447185+0j) (19.483841913429192+0j) 8.924648276682952j
5 0j (4.912646423263124-0j) (11.672398074089152+0j)
6 (4.912646423263124+0j) 0j (19.000435578165046+0j)
7 (11.672398074089152+0j) (19.000435578165046-0j) 0j
--------------------------------------------------
To know more about pandas:
[https://pandas.pydata.org/docs/][1]
Stackoverflow itself is an excellent resource for gathering all way of using pandas.
[1]: https://pandas.pydata.org/docs/
Here column names are defined as 'x' and 'y' in the header.
If the column names are not specified you can add a new header by:
df.columns=['x','y']
after reading the csv file (or text file).
If it already has a header and want to use that name just specify that in the lambdas formula.
Please see:
https://stackoverflow.com/questions/14365542/import-csv-file-as-a-pandas-dataframe
Hope this helps
每当有一个问题看起来像是可以在excel工作表中完成的,并且想要启用python方法来完成它时,我就使用pandas 我想你也可以使用熊猫 下面是读取“columns.txt”文件并输出为“output.csv”的代码,该文件查找每行与其他行的距离,并添加一个新列
import pandas as pd
import cmath
df = pd.read_csv('columns.txt', sep=r"\s+") # read columns.txt into a dataframe, using space as deliimter
df.dropna(inplace=True,axis=1) # multiple whitespaces create NA columns. Better to use csv file
df = df.astype(float) # specify the columsn as type float
print("-"*20 + "Input" + "-"*20)
print(df) #
print("-"*50)
for index, row in df.iterrows():
origin=row # specify first row as origin
'''
Adding distance column
Here we are using a lambda function (same as de used in the question)
and creating a new column called distance
'''
df["distance from row {}".format(index)]=df.apply(lambda row_lambda: cmath.sqrt((origin.x-row_lambda.x)**2 - (origin.y-row_lambda.y)**2), axis=1)
print("-"*20 + "Output" + "-"*20)
print(df)
print("-"*50)
# Save this output as csv file (even excel is possible)
df.to_csv('Output.csv')```
The output will look like:
--------------------Input--------------------
x y
0 -99.9580 -28.84930
1 -71.5378 -26.77280
2 -91.6913 -40.90390
3 -69.0989 -12.95010
4 -79.6443 -9.20575
5 -92.1975 -20.02760
6 -99.7732 -14.26070
7 -80.3767 -18.16040
--------------------------------------------------
--------------------Output--------------------
x y distance from row 0 distance from row 1 \
0 -99.9580 -28.84930 0j (28.344239552155912+0j)
1 -71.5378 -26.77280 (28.344239552155912+0j) 0j
2 -91.6913 -40.90390 8.773542743384796j (14.369257985017867+0j)
3 -69.0989 -12.95010 (26.448052710360358+0j) 13.605837059144871j
4 -79.6443 -9.20575 (5.174683670283624+0j) 15.584797189970107j
5 -92.1975 -20.02760 4.194881481043308j (19.527556965734348+0j)
6 -99.7732 -14.26070 14.587429482948666j (25.31175945583396+0j)
7 -80.3767 -18.16040 (16.40654523292457+0j) (1.9881447256173002+0j)
distance from row 2 distance from row 3 distance from row 4 \
0 8.773542743384796j (26.448052710360358+0j) (5.174683670283624+0j)
1 (14.369257985017867+0j) 13.605837059144871j -15.584797189970107j
2 0j 16.462028935705348j 29.319660714655278j
3 16.462028935705348j 0j (9.858260710566546-0j)
4 29.319660714655278j (9.858260710566546+0j) 0j
5 20.87016203219323j (21.987594586720945+0j) (6.361634445447185+0j)
6 25.387851398454337j (30.646288651809048+0j) (19.483841913429192+0j)
7 19.72933397482034j (10.002077121778257+0j) 8.924648276682952j
distance from row 5 distance from row 6 distance from row 7
0 4.194881481043308j 14.587429482948666j (16.40654523292457+0j)
1 (19.527556965734348-0j) (25.31175945583396-0j) (1.9881447256173002-0j)
2 -20.87016203219323j -25.387851398454337j 19.72933397482034j
3 (21.987594586720945+0j) (30.646288651809048+0j) (10.002077121778257+0j)
4 (6.361634445447185+0j) (19.483841913429192+0j) 8.924648276682952j
5 0j (4.912646423263124-0j) (11.672398074089152+0j)
6 (4.912646423263124+0j) 0j (19.000435578165046+0j)
7 (11.672398074089152+0j) (19.000435578165046-0j) 0j
--------------------------------------------------
To know more about pandas:
[https://pandas.pydata.org/docs/][1]
Stackoverflow itself is an excellent resource for gathering all way of using pandas.
[1]: https://pandas.pydata.org/docs/
仍然得到相同的错误[x,y]=(int(n)表示line.split()中的n)ValueError:int()的文本无效,以10为基数:'-9.0检查更新的解决方案感谢您的帮助我检查并获得了ValueError:math domain error问题在于您的值有一些CA,您试图对负值进行sqr,尝试我的更新我从计算中删除了负值我对你的答案进行了投票,因为你帮助了我。。谢谢但是我仍然有一个问题,那就是当我得到错误数字的结果时,将要计算的点序列。这不是一个距离公式。这是减去差异的平方,而不是相加。我的错误-我认为cmath将非常容易地在这个基础上工作df[“距行{}.format(index)]=df.apply(lambda row_lambda:cmath.sqrt((origin.x-row_lambda.x)**2-(origin y-row_lambda.y)**2),axis=1)什么是
origin
?问题是他想用每一行计算公式,而不仅仅是从第一行开始。我想我已经回答了两次——我要删除这个版本。也许通过标题工作会有所帮助。我已经看到,一旦你开始使用熊猫,它就会变得非常高效和直观。谢谢你的努力。我尝试了代码,但得到了第2行,在df=pd.read_csv('columnss.txt',delimiter=“”)pandas.errors.parserror:Error标记化数据。C错误:第1792行应为1个字段,SAW3您可以将columns.txt保存为csv文件并直接读取。我没有看到使用示例输入时出现的错误。我现在再试一次,我想可能是因为您的数据文件中有空行。我使用了增量变量,但它不会影响代码。。今天我从你那里学到了很多。。谢谢
import pandas as pd
import cmath
df = pd.read_csv('columns.txt', sep=r"\s+") # read columns.txt into a dataframe, using space as deliimter
df.dropna(inplace=True,axis=1) # multiple whitespaces create NA columns. Better to use csv file
df = df.astype(float) # specify the columsn as type float
print("-"*20 + "Input" + "-"*20)
print(df) #
print("-"*50)
for index, row in df.iterrows():
origin=row # specify first row as origin
'''
Adding distance column
Here we are using a lambda function (same as de used in the question)
and creating a new column called distance
'''
df["distance from row {}".format(index)]=df.apply(lambda row_lambda: cmath.sqrt((origin.x-row_lambda.x)**2 - (origin.y-row_lambda.y)**2), axis=1)
print("-"*20 + "Output" + "-"*20)
print(df)
print("-"*50)
# Save this output as csv file (even excel is possible)
df.to_csv('Output.csv')```
The output will look like:
--------------------Input--------------------
x y
0 -99.9580 -28.84930
1 -71.5378 -26.77280
2 -91.6913 -40.90390
3 -69.0989 -12.95010
4 -79.6443 -9.20575
5 -92.1975 -20.02760
6 -99.7732 -14.26070
7 -80.3767 -18.16040
--------------------------------------------------
--------------------Output--------------------
x y distance from row 0 distance from row 1 \
0 -99.9580 -28.84930 0j (28.344239552155912+0j)
1 -71.5378 -26.77280 (28.344239552155912+0j) 0j
2 -91.6913 -40.90390 8.773542743384796j (14.369257985017867+0j)
3 -69.0989 -12.95010 (26.448052710360358+0j) 13.605837059144871j
4 -79.6443 -9.20575 (5.174683670283624+0j) 15.584797189970107j
5 -92.1975 -20.02760 4.194881481043308j (19.527556965734348+0j)
6 -99.7732 -14.26070 14.587429482948666j (25.31175945583396+0j)
7 -80.3767 -18.16040 (16.40654523292457+0j) (1.9881447256173002+0j)
distance from row 2 distance from row 3 distance from row 4 \
0 8.773542743384796j (26.448052710360358+0j) (5.174683670283624+0j)
1 (14.369257985017867+0j) 13.605837059144871j -15.584797189970107j
2 0j 16.462028935705348j 29.319660714655278j
3 16.462028935705348j 0j (9.858260710566546-0j)
4 29.319660714655278j (9.858260710566546+0j) 0j
5 20.87016203219323j (21.987594586720945+0j) (6.361634445447185+0j)
6 25.387851398454337j (30.646288651809048+0j) (19.483841913429192+0j)
7 19.72933397482034j (10.002077121778257+0j) 8.924648276682952j
distance from row 5 distance from row 6 distance from row 7
0 4.194881481043308j 14.587429482948666j (16.40654523292457+0j)
1 (19.527556965734348-0j) (25.31175945583396-0j) (1.9881447256173002-0j)
2 -20.87016203219323j -25.387851398454337j 19.72933397482034j
3 (21.987594586720945+0j) (30.646288651809048+0j) (10.002077121778257+0j)
4 (6.361634445447185+0j) (19.483841913429192+0j) 8.924648276682952j
5 0j (4.912646423263124-0j) (11.672398074089152+0j)
6 (4.912646423263124+0j) 0j (19.000435578165046+0j)
7 (11.672398074089152+0j) (19.000435578165046-0j) 0j
--------------------------------------------------
To know more about pandas:
[https://pandas.pydata.org/docs/][1]
Stackoverflow itself is an excellent resource for gathering all way of using pandas.
[1]: https://pandas.pydata.org/docs/