如何使用 Perl 或 Python 将 EOF 附加到文件?
我正在尝试将数据批量插入到 SQL Server Express 数据库。从 Windows XP 命令提示符执行 bcp 时,出现以下错误:
C:\temp>bcp in -T -f -S Starting copy... SQLState = S1000, NativeError = 0 Error = [Microsoft][SQL Native Client]Unexpected EOF encountered in BCP data-file 0 rows copied. Network packet size (bytes): 4096 Clock Time (ms.) Total : 4391
So, EOF 有问题。如何使用 Perl 或 Python 将正确的 EOF 字符附加到该文件?
I’m trying to bulk insert data to SQL server express database. When doing bcp from Windows XP command prompt, I get the following error:
C:\temp>bcp in -T -f -S Starting copy... SQLState = S1000, NativeError = 0 Error = [Microsoft][SQL Native Client]Unexpected EOF encountered in BCP data-file 0 rows copied. Network packet size (bytes): 4096 Clock Time (ms.) Total : 4391
So, there is a problem with EOF. How to append a correct EOF character to this file using Perl or Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
EOF 是文件结束符。可能发生的情况是文件不完整;软件需要数据,但现在已经没有了。
这些情况会发生在以下情况:
此类情况。
顺便说一句,虽然 EOF 通常只是文件结束符,但确实存在 EOF 字符。使用此方法是因为终端(命令行)输入并不像文件那样真正结束,但有时需要将 EOF 传递给此类实用程序。我不认为它在真实文件中使用,至少不表示文件结束。文件系统非常清楚文件何时结束,它不需要指示器来找出这一点。
编辑 无耻地从 John Machin 提供的评论中复制
它可能会(无意中)发生在真实文件中。它所需要的只是 (1) 数据输入用户错误地键入 Ctrl-Z,在屏幕上看不到任何内容,然后键入预期的 Shift-Z,然后继续,以及 (2) 验证软件(由公司总裁的侄子等编写) )它很乐意在文本字段中接受 Ctrl-anykey,并且您的数据库中有一个小炸弹,只是等待有人对平面文件生成查询。
EOF is End Of File. What probably occurred is that the file is not complete; the software expects data, but there is none to be had anymore.
These kinds of things happen when:
these kinds of things.
By the way, though EOF is usually just an end of file, there does exist an EOF character. This is used because terminal (command line) input doesn't really end like a file does, but it sometimes is necessary to pass an EOF to such a utility. I don't think it's used in real files, at least not to indicate an end of file. The file system knows perfectly well when the file has ended, it doesn't need an indicator to find that out.
EDIT shamelessly copied from a comment provided by John Machin
It can happen (uninentionally) in real files. All it needs is (1) a data-entry user to type Ctrl-Z by mistake, see nothing on the screen, type the intended Shift-Z, and keep going and (2) validation software (written by e.g. the company president's nephew) which happily accepts Ctrl-anykey in text fields and your database has a little bomb in it, just waiting for someone to produce a query to a flat file.
意外的 EOF 意味着 bcp 读取器在期望更多数据时发现了 EOF。该 EOF 可以是:
(1) 实际的物理文件结尾(不再需要读取字节)。这意味着您的数据格式错误。检查文件末尾附近是否有不完整的记录。
或者
(2) 在 Windows 上,以文本模式读取文件的程序遵循通过 MS-DOS 从 CP/M 继承的关于 Ctrl-Z 的古老约定(又名 ^Z 又名 \'x1A' 又名 SUB 又名 SUBSTITUTE)作为从任何文件(而不仅仅是终端)读取时的文件结束标记。这包括 Python——行为由 C stdlib 决定。检查数据中是否有“\x1A”。
更新以清晰的方式响应评论:
在 Notepad++ 中,您可以通过执行“查看”/“显示符号”/“显示所有字符”来使其显示不寻常的字符。您可以通过执行 Ctrl-F、在“查找内容”框中键入 \x1a,然后在“搜索”面板中选择“扩展”单选按钮来进行搜索。
或者,您可以使用一点 Python 来获取第一个 Ctrl-Z 的行号:
您的 .dat 创建位置并不重要。无意的 Ctrl-Z 可能发生在任何操作系统上创建的文件中的任何位置。重要的是它被作为文本文件读取——Windows?砰!
Unexpected EOF means that the bcp reader found an EOF when it was expecting more data. This EOF can be:
(1) the actual physical end-of-file (no more bytes to be read). This means that you have mis-formatted data. Check near the end of your file for an incomplete record.
OR
(2) on Windows, where you are, programs reading a file in text mode honour the ancient convention inherited via MS-DOS from CP/M of regarding Ctrl-Z (aka ^Z aka \'x1A' aka SUB aka SUBSTITUTE) as an end-of-file marker when reading from ANY file, not just a terminal. This includes Python -- the behaviour is determined by the C stdlib. Check for '\x1A' in your data.
Update responding to comments in a legible fashion:
In Notepad++, you can make it display unusual characters by doing View / Show Symbol / Show All Characters. You can search by doing Ctrl-F, typing \x1a in the Find What box, and selecting the Extended radio button in the Search panel.
Or you can with a little bit of Python get the line number of the first Ctrl-Z:
Where your .dat was created doesn't matter. An unintentional Ctrl-Z can happen anywhere in a file created on any operating system. It is where it is being read as a text file that matters -- Windows? Bang!
这不是缺少 EOF 的问题,而是 EOF 存在且不是 bcp 所期望的问题。
我不是 bcp 工具专家,但看起来您的数据文件格式存在一些问题。
This is not a problem with missing EOF, but with EOF that is there and is not expected by bcp.
I am not a bcp tool expert, but it looks like there is some problem with format of your data files.