我在 Julia 中打開了一個檔案:
output_file = open(path_to_file, "a")
我想砍掉檔案的最后六個字符。我以為我可以用chop, ie來做,chop(output_file; tail = 6)但它似乎只適用于Stringtype 而不是IOStream. 我應該怎么做?
julia> rbothpoly(0, 1, [5], 2, 30, "html")
ERROR: MethodError: no method matching chop(::IOStream; tail=6)
Closest candidates are:
chop(::AbstractString; head, tail) at strings/util.jl:164
Stacktrace:
[1]
[...] ERROR STACKTRACE [...]
[3] top-level scope at REPL[37]:1
我是 IOStream 的新手,今天發現了它們。
uj5u.com熱心網友回復:
我在這里找到了我想要的東西,它適用于我的問題:
(tmppath, tmpio) = mktemp()
open(output_filename, "r") do io
for line in eachline(io, keep=true) # keep so the new line isn't chomped
if line == "</pre>\n"
line = "\n"
end
write(tmpio, line)
end
end
close(tmpio)
mv(tmppath, output_filename, force=true)
chmod(output_filename, 0o777)
close(output_file)
也許我的問題可以被標記為重復!
uj5u.com熱心網友回復:
在您的情況下,因為您對檔案末尾進行了一次寫入而不進行任何進一步的讀取或其他操作,您還可以像這樣就地編輯檔案:
function choppre(fname = "data/endinpre.html")
linetodelete = "</pre>\n"
linelength = length(linetodelete)
open(fname, "r ") do f
readuntil(f, linetodelete)
seek(f, position(f) - linelength)
write(f, " "^linelength)
end
end
這會用等長的空格字符覆寫我們希望截斷的文本。我不確定是否有辦法簡單地洗掉該行(而不是用 覆寫它' ')。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/380859.html
上一篇:如何在javascript中使用JSON回應作為陣列
下一篇:在字串中查找特定的數字模式
