[英]To remove last n lines from .asc file and plot using matplotlib
我有一個.asc文件,看起來像這樣(x和y列)
48.069868 11993
49.111533 11824
50.153094 11325
51.194551 11229
52.235905 11318
Date and Time: Wed May 31 14:47:29 2017
Software Version: 4.20.30007.0
Temperature (C): -60
Model: DU970_UVB
在文件末尾有一些字符串行。 所以我用matplotlib作圖
import matplotlib.pyplot as plt
plt.plotfile('600_1%_532e_Mo F1_80sec.asc', delimiter=' ', cols=(0, 1),
names=('col1', 'col2'), )
plt.show()
現在,如果要刪除最后幾行,我可以使用lines [:-1],但是:
我不知道如何使用plotfile
函數執行此操作,但據我所知plotfile
使用函數csv2rec
( docs )。 根據您的情況,您可以執行以下操作:
import matplotlib.pyplot as plt
from matplotlib.mlab import csv2rec
def converter(value):
try:
return float(value)
except ValueError:
pass
if __name__ == "__main__":
data = csv2rec(
"'600_1%_532e_Mo F1_80sec.asc'",
delimiter=" ",
names=("col1", "col2"),
converterd={
"col1": converter,
"col2": converter,
}
)
data = filter(lambda i: i[0], data)
plt.plot(*zip(*data))
plt.show()
如果轉換器函數無法將值轉換為浮點型,則返回None
。 因此,所有不可轉換項目的data
都將None
值。 之后,您可以對其進行過濾。
如果您仔細閱讀文檔,您會發現,
matplotlib.pyplot.plotfile(fname, cols=(0, ),
plotfuncs=None, comments='#', skiprows=0, checkrows=5,
delimiter=',',names=None, subplots=True, newfig=True, **kwargs)
有一個可選參數稱為comment 。 那是什么意思? 這意味着從文件中讀取,但跳過那些注釋行。
你也說過
我不想永久刪除這些行。 只是為了繪制數據點我想刪除它們
因此,您不想刪除它們,但又不想使用它們。 因此,將其更改為評論!
my_file.asc
:
48.069868 11993
49.111533 11824
50.153094 11325
51.194551 11229
52.235905 11318
Date and Time: Wed May 31 14:47:29 2017
Software Version: 4.20.30007.0
Temperature (C): -60
Model: DU970_UVB
因此,邏輯忽略了數字。 僅將字符串形式的行轉換為注釋! 如何使用re
。
tf = open('my_file.asc','r+')
d = tf.readlines()
tf.seek(0)
for line in d:
s=re.search(r'[a-zA-Z]',line)
if s:
tf.write('#'+line)
else:
tf.write(line)
tf.truncate()
tf.close()
所以我在做什么是搜索的每一行,如果該行恰好開始的字符串,然后替換line
用'#'+line
。 ( 將其更改為評論) 。 所以之后,這就是my_file.asc
樣子,
48.069868 11993
49.111533 11824
50.153094 11325
51.194551 11229
52.235905 11318
#Date and Time: Wed May 31 14:47:29 2017
#Software Version: 4.20.30007.0
#Temperature (C): -60
#Model: DU970_UVB
之后,您就獲得了想要的東西,這很簡單。 與您的代碼相同。 這是完整的解決方案工作解決方案
import itertools
import matplotlib.pyplot as plt
import re
tf = open('my_file.asc','r+')
d = tf.readlines()
tf.seek(0)
for line in d:
s=re.search(r'[a-zA-Z]',line)
if s:
tf.write('#'+line)
else:
tf.write(line)
tf.truncate()
tf.close()
import matplotlib.pyplot as plt
plt.plotfile('my_file.asc', delimiter=' ',comments='#', cols=(0, 1),
names=('col1', 'col2'), )
plt.show()
與csv2rec
(在csv2rec
調用的函數)相同, plotfile
除了文件路徑之外,似乎還接受文件句柄(盡管此處未明確提及)。
如果您負擔得起將文件存儲到內存中(請閱讀:您不使用巨大文件),則可以使用StringIO.StringIO
(Python 2.7)或io.StringIO
(Python 3)。
StringIO
是一個類似文件的對象,您可以在其中應用其他邏輯(例如,跳過頁腳行)來復制文件的內容。 一旦產生了這個新對象,就可以將其傳遞給plotfile
。 這是一個例子:
import matplotlib.pyplot as plt
from StringIO import StringIO
# from io import StringIO
footerlines = 5
with open('example.asc', 'r') as fhandle:
temp = StringIO()
for line in fhandle.readlines()[:-footerlines]:
temp.write(line)
# bring the pointer back to the beginning of the file-like object
temp.seek(0)
plt.plotfile(temp, delimiter=' ', cols=(0, 1), names=('col1', 'col2'))
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.