读取csv文件,其中3列为网格,前2列为坐标,第3列为值

时间:2016-04-13 08:53:57

标签: python python-2.7 csv ipython spyder

嗨,我仍然是蟒蛇新手和学习,因为我要去。我正在尝试读取包含3列的CSV文件,前2列是坐标,第3列是值。 bellow是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次检查。示例链接,红色是用于检查所有相邻蓝色标记的值。 example_value check 这是我到目前为止的脚本,希望所有这些都是足够的信息和可理解的不知道我是否可以改变它或应该重新开始以及如何。希望有人可以提供帮助。

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

1 个答案:

答案 0 :(得分:0)

这里有两个问题,导入数据并使用它。最好描述一下你在做什么数据,而不是提供你的整个脚本。请参阅https://stackoverflow.com/help/mcve

关于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 http://www.numpy.org/。这个模块,或者更确切地说是模块集合,可能已经具有了您正在尝试的功能。具体来说,您正在寻找本地最小值和最大值。

一旦你有numpy数组,你会发现其他人试图这样做: Find all local Maxima and Minima when x and y values are given as numpy arrays

此外: Get coordinates of local maxima in 2D array above certain value