Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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列作为网格读取csv文件,前2列作为坐标,第3列作为值_Python_Python 2.7_Csv_Ipython_Spyder - Fatal编程技术网

Python 以3列作为网格读取csv文件,前2列作为坐标,第3列作为值

Python 以3列作为网格读取csv文件,前2列作为坐标,第3列作为值,python,python-2.7,csv,ipython,spyder,Python,Python 2.7,Csv,Ipython,Spyder,大家好,我还是python新手,正在学习。我试图读取一个包含3列的CSV文件,前2列是坐标,第三列是值。下面是CSV文件内容的示例 322000.235 582999.865 149.309 322000.235 582999.615 149.29 322000.235 582999.365 149.276 322000.235 582999.115 149.26 322000.235 582998.865 149.246 322000.235 582998.615 149.245

大家好,我还是python新手,正在学习。我试图读取一个包含3列的CSV文件,前2列是坐标,第三列是值。下面是CSV文件内容的示例

322000.235 582999.865 149.309 
322000.235 582999.615 149.29 
322000.235 582999.365 149.276 
322000.235 582999.115 149.26 
322000.235 582998.865 149.246 
322000.235 582998.615 149.245 
322000.235 582998.365 149.235 
322000.235 582998.115 149.228 
322000.235 582997.865 149.223 
322000.235 582997.615 149.226 
322000.485 582999.865 149.249 
322000.485 582999.615 149.217 
322000.485 582999.365 149.224 
322000.485 582999.115 149.243 
322000.485 582998.865 149.249 
322000.485 582998.615 149.256 
322000.485 582998.365 161.259 
322000.485 582998.115 149.257 
322000.485 582997.865 149.26 
322000.485 582997.615 149.274 
322000.735 582999.865 149.193 
322000.735 582999.615 149.159 
322000.735 582999.365 149.179 
322000.735 582999.115 149.215 
322000.735 582998.865 149.242 
322000.735 582998.615 149.261 
322000.735 582998.365 160.274 
322000.735 582998.115 149.29 
322000.735 582997.865 149.321 
322000.735 582997.615 149.342 
322000.985 582999.865 149.156 
322000.985 582999.615 149.128 
322000.985 582999.365 149.16 
322000.985 582999.115 149.205 
322000.985 582998.865 149.239 
322000.985 582998.615 149.265 
322000.985 582998.365 149.289 
322000.985 582998.115 149.324 
322000.985 582997.865 149.373 
322000.985 582997.615 149.401
我需要它来阅读它如下

(322000.235 582999.865 149.309 )    (322000.485 582999.865 149.249 )    (322000.735 582999.865 149.193 )    (322000.985 582999.865 149.156 )
(322000.235 582999.615 149.29  )    (322000.485 582999.615 149.217 )    (322000.735 582999.615 149.159 )    (322000.985 582999.615 149.128 )
(322000.235 582999.365 149.276 )    (322000.485 582999.365 149.224 )    (322000.735 582999.365 149.179 )    (322000.985 582999.365 149.16  )
(322000.235 582999.115 149.26  )    (322000.485 582999.115 149.243 )    (322000.735 582999.115 149.215 )    (322000.985 582999.115 149.205 )
(322000.235 582998.865 149.246 )    (322000.485 582998.865 149.249 )    (322000.735 582998.865 149.242 )    (322000.985 582998.865 149.239 )
(322000.235 582998.615 149.245 )    (322000.485 582998.615 149.256 )    (322000.735 582998.615 149.261 )    (322000.985 582998.615 149.265 )
(322000.235 582998.365 149.235 )    (322000.485 582998.365 161.259 )    (322000.735 582998.365 160.274 )    (322000.985 582998.365 149.289 )
(322000.235 582998.115 149.228 )    (322000.485 582998.115 149.257 )    (322000.735 582998.115 149.29  )    (322000.985 582998.115 149.324 )
(322000.235 582997.865 149.223 )    (322000.485 582997.865 149.26  )    (322000.735 582997.865 149.321 )    (322000.985 582997.865 149.373 )
(322000.235 582997.615 149.226 )    (322000.485 582997.615 149.274 )    (322000.735 582997.615 149.342 )    (322000.985 582997.615 149.401 )
我把一些东西放在一起,通过第3列,使用.shift(-1)和.shift(1)将值检查到它的相邻值,这可以完成任务,但我得到了不必要的数据,我实际上想要的不仅仅是检查它旁边的值,而是将它作为一个网格检查,相邻值在大多数情况下是4个检查。示例链接,红色是要与所有相邻的蓝色标记一起检查的值。 这是我的脚本,我到目前为止,希望所有这些都是足够的信息和可以理解的不知道我是否可以改变它或应该重新开始,以及如何。希望有人能帮忙

from __future__ import print_function

import pandas as pd
import os
import re


Dir = os.getcwd()
Blks = []
CSV = []



for files in Dir:
    for f in os.listdir(Dir):
        if re.search('.txt', f):
            Blks = [each for each in os.listdir(Dir) if each.endswith('.txt')]
print (Blks)

for files in Dir:
    for f in os.listdir(Dir):
        if re.search('.csv', f):
            CSV = [each for each in os.listdir(Dir) if each.endswith('.csv')]
print (CSV)



limit = 3
tries = 0

while True:
        print ("----------------------------------------------------")
        spikewell = float(raw_input("Please Enter Parameters: "))
        tries += 1
        if tries == 4:
            print ("----------------------------------------------------")
            print ("Entered incorrectly to many times.....Exiting")
            print ("----------------------------------------------------")
            break
        else:
            if spikewell > 50:
               print ("parameters past limit (20)")
               print ("----------------------------------------------------")
               print (tries)
               continue
            elif spikewell < 0:
               print ("Parameters cant be negative")
               print ("----------------------------------------------------")
               print (tries)
               continue
            else:
               spikewell
               print ("Parameters are set")
               print (spikewell)
               print ("Searching files")
               print ("----------------------------------------------------")

        for z in Blks:
            df = pd.read_csv(z, sep=r'\s+', names=['X','Y','Z'])
            z = sum(df['Z'])
            average = z / len(df['Z'])




        for terrain in Blks:
                for df in terrain:
                    df = pd.read_csv(terrain, sep=r'\s+', names=['X','Y','Z'])


                    spike_zleft = df['Z'] - df['Z'].shift(1)
                    spike_zright = df['Z'] - df['Z'].shift(-1)
                    wzdown = -(df['Z'] - df['Z'].shift(-1))
                    wzup_abs = abs(df['Z'] - df['Z'].shift(1))
                    wzdown_abs = abs(wzdown)
                    spikecsv = ('spikes.csv')
                    wellcsv = ('wells.csv')




                    spikes_search = df.loc[(spike_zleft  > spikewell) & (spike_zright > spikewell)]
                    with open(spikecsv, 'a') as f:
                        spikes_search[['X','Y','Z']].to_csv(f, sep='\t', index=False)


                    well_search = df.loc[(wzup_abs > spikewell) & (wzdown > spikewell)]
                    with open(wellcsv, 'a') as f:
                        well_search[['X','Y','Z']].to_csv(f, sep='\t', index=False)

                    print ("----------------------------------------------------")
                    print ('Search completed')
                    if len(spikes_search) == 0:
                        print ("0 SPIKE\S FOUND")
                    elif len(spikes_search) > 0:
                        print (terrain)
                        print (str(len(spikes_search)) + " SPIKE\S FOUND")
                    elif len(spikes_search) > 0:
                        print (str(len(spikes_search)) + " SPIKE\S FOUND")


                    if len(well_search) == 0:
                        print ("0 WELL\S FOUND")
                    elif len(well_search) > 0:
                        print (str(len(well_search)) + " WELL\S FOUND")
                    elif len(well_search) > 0:
                        print (str(len(well_search)) + " WELL\S FOUND")


                    break

        break
from\uuuuu future\uuuuu导入打印功能
作为pd进口熊猫
导入操作系统
进口稀土
Dir=os.getcwd()
Blks=[]
CSV=[]
对于目录中的文件:
对于os.listdir(Dir)中的f:
如果重新搜索('.txt',f):
Blks=[如果each.endswith('.txt'),则os.listdir(Dir)中的每个文件对应于每个文件]
打印(黑色)
对于目录中的文件:
对于os.listdir(Dir)中的f:
如果重新搜索('.csv',f):
CSV=[如果each.endswith('.CSV'),则os.listdir(Dir)中的每个文件对应于每个文件]
打印(CSV)
极限=3
尝试=0
尽管如此:
打印(------------------------------------------------------------)
spikewell=float(原始输入(“请输入参数”))
尝试次数+=1
如果尝试==4:
打印(------------------------------------------------------------)
打印(“多次输入错误…..退出”)
打印(------------------------------------------------------------)
打破
其他:
如果spikewell>50:
打印(“参数超过限制(20)”)
打印(------------------------------------------------------------)
打印(尝试)
持续
elif spikewell<0:
打印(“参数不能为负数”)
打印(------------------------------------------------------------)
打印(尝试)
持续
其他:
斯派克韦尔
打印(“参数已设置”)
打印(斯派克韦尔)
打印(“搜索文件”)
打印(------------------------------------------------------------)
对于Blks中的z:
df=pd.read_csv(z,sep=r'\s+',name=['X','Y','z']))
z=总和(df['z'])
平均值=z/len(df['z'])
对于Blks中的地形:
对于地形中的测向:
df=pd.read_csv(地形,sep=r'\s+',名称=['X','Y','Z']))
spike_zleft=df['Z']-df['Z'].移位(1)
spike_zright=df['Z']-df['Z'].移位(-1)
wzdown=-(df['Z']-df['Z'].shift(-1))
wzup_abs=abs(df['Z']-df['Z'].移位(1))
wzdown_abs=abs(wzdown)
spikecsv=('spikes.csv')
wellcsv=('wells.csv')
尖峰搜索=df.loc[(尖峰左>尖峰井)和(尖峰右>尖峰井)]
将open(a)作为f:
尖峰搜索[['X','Y','Z']]。到csv(f,sep='\t',index=False)
井搜索=df.loc[(wzup\U abs>spikewell)和(wzdown>spikewell)]
将open(wellcsv,'a')作为f:
well_search[['X','Y','Z']]。到_csv(f,sep='\t',index=False)
打印(------------------------------------------------------------)
打印('搜索已完成')
如果len(尖峰搜索)==0:
打印(“找到0个峰值”)
elif len(峰值搜索)>0:
打印(地形)
打印(str(len(spikes\u search))+“SPIKE\S FOUND”)
elif len(峰值搜索)>0:
打印(str(len(spikes\u search))+“SPIKE\S FOUND”)
如果len(井搜索)=0:
打印(“找到0个井”)
elif len(井搜索)>0:
打印(str(len(well_search))+“well\S FOUND”)
elif len(井搜索)>0:
打印(str(len(well_search))+“well\S FOUND”)
打破
打破

这里有两个问题,导入数据和使用数据。与其提供整个脚本,不如描述一下您对数据所做的操作。请看

关于CSV输入,请使用
CSV
模块

import csv

with open('FILENAME','r') as f:
    data = []
    readr = csv.reader(f)
    for line in readr:
        data.append([float(i) for i in line])
但是你的熊猫代码已经做到了这一点

如果您正在使用数组进行数值计算(看起来您是这样的),那么您应该查看
numpy
。这个模块,或者更确切地说是模块的集合,可能已经有了一个函数来完成您要做的事情。具体来说,您正在寻找局部极小值和极大值

一旦你有了numpy阵列,你会发现其他人也在尝试这样做:

此外:

我至少不清楚这个问题?你到底想在这里做什么?你可能想看看旋转:@tfv谢谢,这看起来很有趣,可能会对我有所帮助。@Anvesh我试图搜索3列,第三列作为我的值,前2列只是“坐标”或位置。就像x和y到网格一样。第三列是值。但是我想找出这些值的高变化,但是将它们与相邻的值进行比较。。希望这更有意义。@dodell,我使用数据的目的是地形数据、地理信息系统、高程坐标系,我试图找到超过5米的特定标准的尖峰。很抱歉发布整个脚本,不是因为大小而混淆,是吗