我對編碼和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
您應該提供導致錯誤的代碼。 '不是所有在字符串格式化過程中轉換的參數都意味着有一個字符串格式化操作,''不能連接'str'和'int'對象意味着有一個連接,這兩個連接都不在你向我們展示的代碼中。 – gmoshkin
您期望在輸出中有多少行 - 只有一列使用column3 == 108.09?或者你是否也需要上一行和下一行?請提供預期產出的例子。 – MaxU
@gmoshkin @ MaxU對不起,如果給了一點點信息stil學習如何所有這些工作,列1和2實際上是(x,y)座標和最後一列是z值(高度值)。所以基本上我想創建一些東西,在其他點之間找到示例108.09,然後返回該點的x和y座標。所以在一個新的txt文件名爲尖峯的結尾處的輸出將具有特定尖峯z值的x和y:98015.985 -4922344.462希望這有助於 –