我正在尝试从Python 中的Common Crawl解析 .warc 文件。
由于文件很大,我想从前几条记录的样本/子集开始。
如何截断文件以仅包含前 X 行,同时保留现有的换行符/回车符?
这是我已经尝试过的:
head -n 250 oldfile > newfile
这删除了解析文件所需的一些返回。如果我尝试在我的 Hadoop 作业中使用这个文件(使用warc
包读取它),这是我得到的错误:Traceback (most recent call last): File "test.py", line 46, in <module> TagGrabber.run() File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/mrjob/job.py", line 461, in run mr_job.execute() File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/mrjob/job.py", line 479, in execute super(MRJob, self).execute() File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/mrjob/launch.py", line 151, in execute self.run_job() File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/mrjob/launch.py", line 214, in run_job runner.run() File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/mrjob/runner.py", line 464, in run self._run() File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/mrjob/sim.py", line 173, in _run self._invoke_step(step_num, 'mapper') File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/mrjob/sim.py", line 264, in _invoke_step self.per_step_runner_finish(step_num) File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/mrjob/local.py", line 152, in per_step_runner_finish self._wait_for_process(proc_dict, step_num) File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/mrjob/local.py", line 268, in _wait_for_process (proc_dict['args'], returncode, ''.join(tb_lines))) Exception: Command ['sh', '-ex', 'setup-wrapper.sh', '/var/cc-mrjob/venv/bin/python', 'test.py', '--step-num=0', '--mapper', '/tmp/test.root.20150520.071726.549519/input_part-00000'] returned non-zero exit status 1: Traceback (most recent call last): File "test.py", line 46, in <module> TagGrabber.run() File "/tmp/test.root.20150520.071726.549519/job_local_dir/0/mapper/0/mrjob.tar.gz/mrjob/job.py", line 461, in run mr_job.execute() File "/tmp/test.root.20150520.071726.549519/job_local_dir/0/mapper/0/mrjob.tar.gz/mrjob/job.py", line 470, in execute self.run_mapper(self.options.step_num) File "/tmp/test.root.20150520.071726.549519/job_local_dir/0/mapper/0/mrjob.tar.gz/mrjob/job.py", line 535, in run_mapper for out_key, out_value in mapper(key, value) or (): File "/var/cc-mrjob/mrcc.py", line 33, in mapper for i, record in enumerate(f): File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/warc/warc.py", line 390, in __iter__ record = self.read_record() File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/warc/warc.py", line 373, in read_record header = self.read_header(fileobj) File "/var/cc-mrjob/venv/local/lib/python2.7/site-packages/warc/warc.py", line 331, in read_header raise IOError("Bad version line: %r" % version_line) IOError: Bad version line: 'WARC/1.0\n'
与 #1 相同,但使用
tail
命令- 与 #1 相同,但使用
tr
orsed
after 替换任何丢失的换行符或^M
(回车符)字符。这会导致warc
包仍然抱怨预期的回车或换行符没有到位。 unix2dos oldfile