Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何将txt文件中的数据读入avergae calculatins Python的行和列_Python 3.x_Post Processing - Fatal编程技术网

Python 3.x 如何将txt文件中的数据读入avergae calculatins Python的行和列

Python 3.x 如何将txt文件中的数据读入avergae calculatins Python的行和列,python-3.x,post-processing,Python 3.x,Post Processing,所以我使用PIV(openPiv)来处理图像。处理并保存数据后,我得到一个.txt文件,其中有三列x y V: x:44881324488132 y:100 100 50 50 V:502367 我需要画一个x对V的图,但是对于相同的x值,我有2个V值,所以我想平均2。 因此,我试图用python编写一个代码,可以读取文件中x的值,并返回所有相关的V值,以便对它们求和并求平均值。假设它的读数为x=44,转到文件,然后对x=44,v=5和3进行se 我会非常感激你的帮助 多谢各位 fl=open(

所以我使用PIV(openPiv)来处理图像。处理并保存数据后,我得到一个.txt文件,其中有三列x y V:

x:44881324488132

y:100 100 50 50

V:502367

我需要画一个x对V的图,但是对于相同的x值,我有2个V值,所以我想平均2。 因此,我试图用python编写一个代码,可以读取文件中x的值,并返回所有相关的V值,以便对它们求和并求平均值。假设它的读数为x=44,转到文件,然后对x=44,v=5和3进行se

我会非常感激你的帮助 多谢各位

fl=open("fileq.txt",'r+')
read_file=fl.read()
ls2=[]
x=0
y=1
V=2
ls_of_ls=[]
sepr=read_file.split('\n')
for items in sepr:
    items=items.split(':')[1]
    ls2.append(items)
for items in ls2:
    ls_of_ls.append(list(map(int,items.split())))
len_inp=len(ls_of_ls[x])
k=int(input("for value x: "))
print("V= ",end='')
for i in range(len_inp):
    if k==ls_of_ls[x][i]:
        print(ls_of_ls[V][i],end=" ")
我已经根据文本文件中的给定格式编写了此代码,即

x:44881324488132

y:100 100 50 50

V:502367

只需将fileq.txt替换为您的文件

如果你想要垂直格式的代码,我可以帮你

import pandas as pd
file=pd.read_csv('file.csv', sep='\t')
velocity_along_x=file.groupby('x')['V'].mean()
velocity_along_x.plot()

我找到了一种更简单的使用Pandas的方法

非常感谢,它真的很有帮助,我从一个类似的逻辑开始,但不知道如何继续(由于我在python方面的经验有限);我的文件确实是垂直格式的,但不用担心,我会努力让它自己工作(需要提高我的技能)。再次感谢你的帮助我真的很感激