2016-03-01 74 views
1

我對編碼和python非常陌生,並且認爲我在咬人關閉更多然後我可以咀嚼,但我試圖創建程序,讀取3列信息的txt文件,然後將這些列並列出它們。 然後我想創建一個條件,它將第三列的行值與上下行進行比較,如果值的差值大於5,則它將複製第3列中找到該值的第1列和第2列並將其附加到名爲spikes的新列表中,我希望可以創建一個新的單獨的txt文件。名稱爲「xyz_test.txt」 txt文件值我有 例如:用3列打開txt文件,檢查第3列的差異爲5並將該行的第1列和第2列返回到新的txt文件

98015.985 -4922343.462 101.098 
98015.985 -4922343.712 101.098 
98015.985 -4922343.962 101.093 
98015.985 -4922344.212 101.089 
98015.985 -4922344.462 108.09 
98015.985 -4922344.712 101.095 
98015.985 -4922344.962 101.093 
98015.985 -4922345.212 101.083 
98015.985 -4922345.462 101.081 

到目前爲止,我能得到並且弄清楚是以下幾點:

import csv,math listxy = [] listz = [] spikes = [] files = 
list(csv.reader(open('xyz_test.txt', 'rb'), delimiter='\t')) 

for z in files: 
    listxy = z[0],z[1] 
    listz = z[2] 
    print listz 

結果我得到的是以下幾點:

101.098 
101.098 
101.093 
101.089 
108.09 
101.095 
101.093 
101.083 
101.081 

現在我已經嘗試運行情況,首先發現,在列表中的一個數字,就是不同的是高於5到它的上面和下面的數字,但不斷收到以下錯誤: 「不是字符串格式化過程中轉換所有參數」
「不能連接‘海峽’和‘廉政’對象」

誰能幫我對此。

Thanks for all the help from everyone, learned allot. i have changed the code to fit what i need , here is what i ended up with. still tweaking , have to create something that sorts the values and loop through several txt files but this is what have so far:

from __future__ import print_function 


import pandas as pd 
# sets dipslay to larger extent 
#pd.set_option('display.height', 10000000) 
#pd.set_option('display.max_rows', 5000000) 
#pd.set_option('display.max_columns', 50) 
#pd.set_option('display.width', 10000) 

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 ("----------------------------------------------------") 





     terrain = "1_tile_test.txt" 
     for df in terrain: 
      df = pd.read_csv('1_tile_test.txt', sep=r'\s+', names=['____x____ ','____y____ ','____z____']) 
# print orginal data frame (for testing) 

# get spikes's coordinates 
# df['col3'].shift(1) - previous value of the 'col3' column 
# df['col3'].shift(-1) - next value of the 'col3' column 
      spikes = df.loc[(df['____z____'] - df['____z____'].shift(1) > spikewell) & \ 
      (df['____z____'] - df['____z____'].shift(-1) > spikewell)] 
      wells = df.loc[-((df['____z____'] - df['____z____'].shift(1) > spikewell)) & \ 
      -((df['____z____'] - df['____z____'].shift(-1)) > -spikewell)] 
# print and save spikes 

    # print(spikes[['col1', 'col2','col3']]) 
    # print(spikes2[['col1', 'col2','col3']]) 
    # print(wells[['col1', 'col2','col3']]) 
    # print(wells2[['col1', 'col2','col3']]) 

      spikes[['____x____ ','____y____ ','____z____']].to_csv('spikes.txt', sep='\t', index=False) 
      #spikes2[['____x____ ','____y____ ','____z____']].to_csv('spikes.txt', sep='\t', index=False) 
      wells[['____x____ ','____y____ ','____z____']].to_csv('wells.txt', sep='\t', index=False) 
      #wells2[['____x____ ','____y____ ','____z____']].to_csv('wells.txt', sep='\t', index=False) 
      print ("----------------------------------------------------") 
      print ('Search completed') 
      break 

     break 
+0

您應該提供導致錯誤的代碼。 '不是所有在字符串格式化過程中轉換的參數都意味着有一個字符串格式化操作,''不能連接'str'和'int'對象意味着有一個連接,這兩個連接都不在你向我們展示的代碼中。 – gmoshkin

+0

您期望在輸出中有多少行 - 只有一列使用column3 == 108.09?或者你是否也需要上一行和下一行?請提供預期產出的例子。 – MaxU

+0

@gmoshkin @ MaxU對不起,如果給了一點點信息stil學習如何所有這些工作,列1和2實際上是(x,y)座標和最後一列是z值(高度值)。所以基本上我想創建一些東西,在其他點之間找到示例108.09,然後返回該點的x和y座標。所以在一個新的txt文件名爲尖峯的結尾處的輸出將具有特定尖峯z值的x和y:98015.985 -4922344.462希望這有助於 –

回答

0

你可能要採取的pandas

輸入數據細看(用於測試目的,我已經添加了一排用[COL3 == 111.110):

98015.985 -4922343.462 101.098 
98015.985 -4922343.712 101.098 
98015.985 -4922343.962 101.093 
98015.985 -4922344.212 101.089 
98015.985 -4922344.462 108.09 
98015.985 -4922344.712 101.095 
98015.985 -4922344.962 101.093 
98015.985 -4922345.212 101.083 
98015.985 -4922344.462 111.110 
98015.985 -4922345.462 101.081 

代碼:

from __future__ import print_function 

import pandas as pd 

df = pd.read_csv('data.csv', sep=r'\s+', names=['col1','col2','col3']) 
# print orginal data frame (for testing) 
print(df) 

# get spikes's coordinates 
# df['col3'].shift(1) - previous value of the 'col3' column 
# df['col3'].shift(-1) - next value of the 'col3' column 
spikes = df.loc[(df['col3'] - df['col3'].shift(1) > 5) & (df['col3'] - df['col3'].shift(-1) > 5)] 

# print and save spikes 
print(spikes[['col1', 'col2']]) 
spikes[['col1', 'col2']].to_csv('spikes.csv', sep='\t', index=False) 

輸出:

 col1   col2  col3 
0 98015.985 -4922343.462 101.098 
1 98015.985 -4922343.712 101.098 
2 98015.985 -4922343.962 101.093 
3 98015.985 -4922344.212 101.089 
4 98015.985 -4922344.462 108.090 
5 98015.985 -4922344.712 101.095 
6 98015.985 -4922344.962 101.093 
7 98015.985 -4922345.212 101.083 
8 98015.985 -4922344.462 111.110 
9 98015.985 -4922345.462 101.081 
     col1   col2 
4 98015.985 -4922344.462 
8 98015.985 -4922344.462 

尖峯。csv:

col1 col2 
98015.985 -4922344.462 
98015.985 -4922344.462 
+0

它說:「沒有名爲pandas的模塊」 –

+0

@EdwinPage,你將不得不安裝它:「pip install pandas」。 http://pandas.pydata.org/pandas-docs/stable/install.html – MaxU

+0

@ MaxU,我得到它安裝!,我跑了一個測試,它的工作原理,我試了一些小列表測試,這很好上一行和下一行都低於5,但是如果上一行或下一行中的一行不大於5,那麼它就會發出接縫而不能拍攝高峯。我嘗試將尖峯改爲「if」功能,但告訴我無效的語法,並嘗試使用「或」而不是「&」,它給了我「一個系列的真值不明確,使用a.empty,a.bool(),a.item(),a。 any()或a.all()。「不知道什麼即時失蹤 –

1

下面是一個例子:

import csv 

def is_spike(three): 
    first, second, third = three 
    return abs(float(first[2]) - float(second[2])) > 5 and abs(float(second[2]) - float(third[2])) > 5 

with open("yourcsvfile.csv") as csvfile: 
    reader = csv.reader(csvfile) 
    rows = list(reader) 
    threes = zip(rows, rows[1:], rows[2:]) 
    spikes = [three for three in threes if is_spike(three)] 

print spikes 

輸出(中間行是 「秒殺」):

[(['98015.985', '-4922344.212', '101.089'], ['98015.985', '-4922344.462', '108.09'], ['98015.985', '-4922344.712', '101.095'])]

演練:

首先我們先讀整行數據使用分割的csv模塊他們爲我們。確保正確設置分隔符。你也可以手動讀取它們,但這是更通用的。

其次,我們將所有的threes(如三行)壓縮並檢查它們是否使用is_spike函數形成一個「尖峯」,這很簡單。

祝你好運。

+0

@ Reut感謝您的回覆!我試過你的代碼,但一直給我一個錯誤message.Traceback(最近呼叫最後): 文件「I:\ test \ Spike2.py」,第14行,在 spikes = [三個三中三個如果is_spike返回abs(float(first [2]) - float(second [2]))> 5和abs(float(second [3])] is_spike 中的文件「I:\ test \ Spike2.py」 2]) - 浮動(第三[2]))> 5 IndexError:列表索引超出範圍 >>>不知道什麼做不正確。 (「file.txt」) –

相關問題