- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 100 GB 的文本文件,它是来自数据库的 BCP 转储。当我尝试使用 BULK INSERT
导入它时,我在第 219506324 行遇到了一个神秘错误。在解决这个问题之前,我希望看到这一行,但是我最喜欢的方法是
import linecache
print linecache.getline(filename, linenumber)
正在抛出一个MemoryError
。有趣的是the manual says “这个函数永远不会抛出异常。” 在这个大文件上,它在我尝试读取第 1 行时抛出一个异常,并且我有大约 6GB 的可用 RAM...
我想知道到达那条无法到达的线路的最优雅方法是什么。可用的工具有 Python 2、Python 3 和 C# 4 (Visual Studio 2010)。是的,我知道我总是可以做类似的事情
var line = 0;
using (var stream = new StreamReader(File.OpenRead(@"s:\source\transactions.dat")))
{
while (++line < 219506324) stream.ReadLine(); //waste some cycles
Console.WriteLine(stream.ReadLine());
}
哪个可行,但我怀疑这是最优雅的方式。
编辑: 我正在等待关闭此线程,因为包含该文件的硬盘现在正被另一个进程使用。我将测试建议的方法和报告时间。谢谢大家的建议和意见。
结果在 我实现了 Gabes 和 Alexes 方法以查看哪个更快。如果我做错了什么,请告诉我。我将使用 Gabe 建议的方法在我的 100GB 文件中获取第 1000 万行,然后使用 Alex 建议的方法,我将其粗略地翻译成 C#...我自己唯一添加的是首先阅读 300 MB 文件到内存只是为了清除硬盘缓存。
const string file = @"x:\....dat"; // 100 GB file
const string otherFile = @"x:\....dat"; // 300 MB file
const int linenumber = 10000000;
ClearHDDCache(otherFile);
GabeMethod(file, linenumber); //Gabe's method
ClearHDDCache(otherFile);
AlexMethod(file, linenumber); //Alex's method
// Results
// Gabe's method: 8290 (ms)
// Alex's method: 13455 (ms)
gabe的方法实现如下:
var gabe = new Stopwatch();
gabe.Start();
var data = File.ReadLines(file).ElementAt(linenumber - 1);
gabe.Stop();
Console.WriteLine("Gabe's method: {0} (ms)", gabe.ElapsedMilliseconds);
虽然 Alex 的方法有点诡异:
var alex = new Stopwatch();
alex.Start();
const int buffersize = 100 * 1024; //bytes
var buffer = new byte[buffersize];
var counter = 0;
using (var filestream = File.OpenRead(file))
{
while (true) // Cutting corners here...
{
filestream.Read(buffer, 0, buffersize);
//At this point we could probably launch an async read into the next chunk...
var linesread = buffer.Count(b => b == 10); //10 is ASCII linebreak.
if (counter + linesread >= linenumber) break;
counter += linesread;
}
}
//The downside of this method is that we have to assume that the line fit into the buffer, or do something clever...er
var data = new ASCIIEncoding().GetString(buffer).Split('\n').ElementAt(linenumber - counter - 1);
alex.Stop();
Console.WriteLine("Alex's method: {0} (ms)", alex.ElapsedMilliseconds);
因此,除非 Alex 愿意发表评论,否则我会将 Gabe 的解决方案标记为已接受。
最佳答案
这是我在 C# 中的优雅版本:
Console.Write(File.ReadLines(@"s:\source\transactions.dat").ElementAt(219506323));
或更一般的:
Console.Write(File.ReadLines(filename).ElementAt(linenumber - 1));
当然,您可能希望在给定行前后显示一些上下文:
Console.Write(string.Join("\n",
File.ReadLines(filename).Skip(linenumber - 5).Take(10)));
或更流利地说:
File
.ReadLines(filename)
.Skip(linenumber - 5)
.Take(10)
.AsObservable()
.Do(Console.WriteLine);
顺便说一句,linecache
模块对大文件没有任何巧妙处理。它只是将整个内容读入,并将其全部保存在内存中。它捕获的唯一异常是与 I/O 相关的(无法访问文件、找不到文件等)。这是代码的重要部分:
fp = open(fullname, 'rU')
lines = fp.readlines()
fp.close()
换句话说,它试图将整个 100GB 的文件放入 6GB 的 RAM 中!手册应该说的可能是“此函数永远不会抛出异常如果它无法访问文件。”
关于c# - 读取文件特定行号的有效方法。 (奖金 : Python Manual Misprint),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3589327/
关闭。这个问题需要更多 focused .它目前不接受答案。 想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this q
我们如何手动启动滑动 Jquery 事件(例如用于点击事件的 $('div').click(); )? $('div').live('swipeleft'); 无法执行此操作。 最佳答案 根据您正在使
我想做一个std::function像可以处理存储多个重载的对象。 语法有点像这样:my_function . 或者,更明确地说: template struct type_list {}; temp
1. macvlan 简介 在 macvlan 出现之前,我们只能为一块以太网卡添加多个 ip 地址,却不能添加多个 mac 地址,因为 mac 地址正是通过其全球唯一性来标识一块以太网卡的,即便
我只想在 shell(或 perl)脚本中获取 MAN 页面的特定部分并打印它。 例如:仅 MAN 页面的 SYNOPSIS 部分 有没有标准的方法来做到这一点? 问候, 阿南丹 最佳答案 据我所知,
我正在使用以下代码为 Woocommerce 创建一个新的自定义订单状态。我遇到的问题是,在管理员中,状态显示为无样式。如何以类似于“处理”的方式设置自定义状态的样式? /* Adding a new
我希望在 MOSS 2007 中创建一个“在线手册”站点模板,以便创建多个站点,这些站点将保存有关一系列主题的静态信息。 我应该使用什么网站模板,我目前正在考虑发布网站,但 Wiki 可能有用? 我应
简单来说,我在客户的整个网站上使用外部 PHP 脚本来实现各种目的,例如获取搜索结果、更新内容等。 我将这些脚本保存在一个目录中: www.domain.com/scripts/scriptname0
在具有 Spring MVC 和 Spring Security 的 Web 应用程序中。 有没有办法手动设置 UserPrincipal? 我需要通过我的 web 应用程序的管理部分切换到另一个用户
我发现我可以使用migrations.RunSQL('some sql')为django项目中的postgres数据库设置列的默认值。 我目前正在通过添加列,makemigrations,然后删除列,
我的 web 应用程序(Spring3 + Hibernate3)始终使用使用 @Transactional 注释的服务类以及以下配置: 现在...我正在使用
我们是否可以手动登录用户并创建一个 TWTRSession 以便继续与 TwitterKit 一起使用?我已经尝试过: NSDictionary *twSessionInfo = [self.acco
我有这个功能: int cipher_file(char *file_path, uint8_t *key, int key_size){ FILE *file; size_t rea
仅当单击按钮 #collapse_init 时,我才能根据 active bool 变量将按钮上的文本更改为“全部显示”或“全部隐藏” 当我手动隐藏/折叠选项卡(一个接一个)时,按钮上的文本没有改变,
我正在尝试从 OpenCV 中重现 projectPoints() 方法的行为。 下面两张图中,红/绿/蓝轴是用OpenCV的方法得到的,而洋红/黄/青轴是用我自己的方法得到的: 图片1 图片2 用我
鉴于 LISP 显然可以用“LISP 微型手册”的“10 条规则”来表达 [1] [2] (这是真的吗?),是否有类似简洁的Scheme描述? 最佳答案 这里麦卡锡的“10 条规则”本质上是操作语义的
手动,我的意思是代码好像我需要迎合,就好像对象不是我期望的那样。 例如,我正在使用中继器。作为其中的一部分,我正在操纵 DataSource 属性。为此,我必须先检查以下内容: this.DataSo
这个问题在这里已经有了答案: Spring Data JPA Update @Query not updating? (5 个答案) 关闭 2 年前。 社区在 去年 审查了是否重新打开这个问题并将其
我真的是 Spring 的新手我正在使用 JSF+Hibernate+Spring 开发一个简单的 JEE 应用程序我在尝试更新 DAO 上的值时遇到了一些问题我确定问题与 Spring 配置 xml
我想对在Web应用程序中在服务器和客户端之间来回传输的数据进行加密。我会使用SSL,但是需要证书和专用IP地址。获得证书没有问题,但专用IP需要我升级到企业托管计划,该计划是在Web主机上每月支付20
我是一名优秀的程序员,十分优秀!