将大文本文件输出解析为另一个文本文件

 baby小明君 发布于 2023-02-09 18:54

我想解析一个大文本文件,如果该行包含某个子字符串,则将该行附加到我的新文本文件中.我需要内存使用率最低的解决方案,这是我到目前为止,评论是我需要帮助添加:

.
.
.
if (File.ReadLines(filepath).Any(line => line.Contains(myXML.searchSTRING)))
{

// code to grab that line and append it to the a new text file 
// if new text file doesn't exist then create it.
// All text files im parsing have the same header, I want to grab
// the third line and use it as my new text file header. 
// Only write the header once, I do not want it written every time a new 
// text file is opened for parsing 

}

samjudson.. 7

试试:

var count = 1;
File.WriteAllLines(newFilePath, 
  File.ReadLines(filepath)
  .Where(count++ == 3 || l => l.Contains(myXML.searchSTRING))
);

双方WriteAllLines()ReadLines()用枚举,所以应该有相对较低的内存使用情况.

我不知道你怎么知道只写一次标题,这取决于你如何打开可用的文件列表.他们在一个阵列?如果是这样,将File.WriteAllLines调用包装在该数组周围的foreach循环中.

1 个回答
  • 试试:

    var count = 1;
    File.WriteAllLines(newFilePath, 
      File.ReadLines(filepath)
      .Where(count++ == 3 || l => l.Contains(myXML.searchSTRING))
    );
    

    双方WriteAllLines()ReadLines()用枚举,所以应该有相对较低的内存使用情况.

    我不知道你怎么知道只写一次标题,这取决于你如何打开可用的文件列表.他们在一个阵列?如果是这样,将File.WriteAllLines调用包装在该数组周围的foreach循环中.

    2023-02-09 18:59 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有