R语言 删除txt文件的前n行

bbuxkriu  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(80)

我有这个txt文件:

c("\\f0\\fs24 \\cf0 <message messageId=\"12450\" replyToMessageId=\"0\" receivedAt=\"2023-06-12T08:42:31Z\" text=\"&gt; GBPJPY SELL 175.41&#xA;&#xA;SL:  175.955&#xA;TP:  174.382&#xA;--Trade by Grace\" />\\", 
"", "  <message messageId=\"12448\" replyToMessageId=\"12442\" receivedAt=\"2023-06-12T02:38:51Z\" text=\"&gt; SET SL TO 1.32979\" />\\", 
"  <message messageId=\"12447\" replyToMessageId=\"12442\" receivedAt=\"2023-06-12T02:38:51Z\" text=\"&gt; USDCAD CLOSE HALF 33  PIPS PROFIT \" />\\", 
"  <message messageId=\"12446\" replyToMessageId=\"12430\" receivedAt=\"2023-06-12T00:42:27Z\" text=\"&gt; EURAUD RUNNING 105+ PIPS PROFIT &#xA;SMALL LOT HOLDER CAN FULL CLOSE WITH PROFIT \" />\\", 
"  <message messageId=\"12442\" replyToMessageId=\"0\" receivedAt=\"2023-06-09T14:42:21Z\" text=\"&gt; USDCAD BUY 1.3328&#xA;&#xA;SL:  1.31655&#xA;TP:  1.34473&#xA;--Trade by Jason\" />\\", 
"  <message messageId=\"12441\" replyToMessageId=\"12430\" receivedAt=\"2023-06-09T14:25:46Z\" text=\"&gt; EURAUD CLOSE PARTIAL 69  PIPS PROFIT  MOVE SL TO ENTRY\" />\\", 
"  <message messageId=\"12440\" replyToMessageId=\"12436\" receivedAt=\"2023-06-09T12:33:37Z\" text=\"&gt; SET SL TO 175.401\" />\\", 
"  <message messageId=\"12439\" replyToMessageId=\"12436\" receivedAt=\"2023-06-09T12:33:36Z\" text=\"&gt; GBPJPY CLOSE HALF 34  PIPS PROFIT \" />\\", 
"  <message messageId=\"12438\" replyToMessageId=\"12430\" receivedAt=\"2023-06-09T12:32:57Z\" text=\"&gt; SET SL TO 1.60599\" />\\"
)

字符串
如何删除前5行而不再次阅读文件?

ymdaylpp

ymdaylpp1#

你不能在没有阅读文件的情况下从文件中删除行。但是,你可以用减去前5行的内容覆盖文件。下面是你如何在Python中做到这一点:

with open('yourfile.txt', 'r') as file:
    lines = file.readlines()

with open('yourfile.txt', 'w') as file:
    file.writelines(lines[5:])

字符串
在R中,你可以使用readLines函数将文件读入一个vector,然后使用writeLines函数将修改后的vector写回文件。下面是你如何做到这一点:

lines <- readLines("yourfile.txt")
writeLines(lines[-(1:5)], "yourfile.txt")

33qvvth1

33qvvth12#

我们可以简单地做:

text_data <- text_data[-(1:5)]

字符串
这种方法将删除第1-5行。如果数据已经存储在变量中,则不需要重新加载它。

相关问题