删除文本文件中的行
我拥有的以下文本文件的示例是:
> 1 -4.6 -4.6 -7.6
>
> 2 -1.7 -3.8 -3.1
>
> 3 -1.6 -1.6 -3.1
文本文件中的数据由制表符分隔,第一列指示位置。
我需要迭代文本文件中除第 0 列之外的每个值并找到最小值。
一旦找到最低值,则需要将该值与列名称和位置一起写入新的文本文件。第 0 列的名称为“position”,第 1 列的名称为“fifteen”,第 2 列的名称为“sixteen”,第 3 列的名称为“seventeen”,
例如,上述数据中的最小值为“-7.6”,位于名称为“seventeen”的第 3 列中”。因此,“7.6”、“seventeen”及其位置值(在本例中为 1)需要写入新的文本文件。
然后我需要从上面的文本文件中删除一些行。
EG,上面的最低值是“-7.6”并且在位置“1”处找到并且在第3列中找到,其名称为“十七”。因此,我需要从文本文件中删除从位置 1 开始(包括位置 1)的 17 行,
因此找到最低值的列表示需要删除的行数,找到它的位置表示该行的起始点。删除
A sample of the following text file i have is:
> 1 -4.6 -4.6 -7.6
>
> 2 -1.7 -3.8 -3.1
>
> 3 -1.6 -1.6 -3.1
the data is separated by tabs in the text file and the first column indicates the position.
I need to iterate through every value in the text file apart from column 0 and find the lowest value.
once the lowest value has been found that value needs to be written to a new text file along with the column name and position. Column 0 has the name "position" Column 1 "fifteen", column 2 "sixteen" and column 3 "seventeen"
for example the lowest value in the above data is "-7.6" and is in column 3 which has the name "seventeen". Therefore "7.6", "seventeen" and its position value which in this case is 1 need to be written to the new text file.
I then need a number of rows deleted from the above text file.
E.G. the lowest value above is "-7.6" and is found at position "1" and is found in column 3 which as the name "seventeen". I therefore need seventeen rows deleted from the text file starting from and including position 1
so the the column in which the lowest value is found denotes the amount of rows that needs to be deleted and the position it is found at states the start point of the deletion
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
打开此文件进行读取,打开另一个文件进行写入,然后复制所有与过滤器不匹配的行:
Open this file for reading, another file for writing, and copy all the lines that don't match the filter:
这是我认为你想要的东西(尽管你的要求有点难以遵循):
我不清楚的事情:
Here's a stab at what I think you wanted (though your requirements were kind of difficult to follow):
Things that weren't clear to me: