Python 使用 open() 时出现“ValueError: 嵌入空字符”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33977519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"ValueError: embedded null character" when using open()
提问by Erica
I am taking python at my college and I am stuck with my current assignment. We are supposed to take 2 files and compare them. I am simply trying to open the files so I can use them but I keep getting the error "ValueError: embedded null character"
我在我的大学学习 Python,但我目前的任务很困难。我们应该取 2 个文件并比较它们。我只是想打开文件,以便我可以使用它们,但我不断收到错误消息"ValueError: embedded null character"
file1 = input("Enter the name of the first file: ")
file1_open = open(file1)
file1_content = file1_open.read()
What does this error mean?
这个错误是什么意思?
回答by stonebig
Default encoding of files for Python 3.5 is 'utf-8'.
Python 3.5 的默认文件编码是“utf-8”。
Default encoding of files for Windows tends to be something else.
Windows 文件的默认编码往往是别的东西。
If you intend to open two text files, you may try this:
如果你打算打开两个文本文件,你可以试试这个:
import locale
locale.getdefaultlocale()
file1 = input("Enter the name of the first file: ")
file1_open = open(file1, encoding=locale.getdefaultlocale()[1])
file1_content = file1_open.read()
There should be some automatic detection in the standard library.
标准库中应该有一些自动检测。
Otherwise you may create your own:
否则,您可以创建自己的:
def guess_encoding(csv_file):
"""guess the encoding of the given file"""
import io
import locale
with io.open(csv_file, "rb") as f:
data = f.read(5)
if data.startswith(b"\xEF\xBB\xBF"): # UTF-8 with a "BOM"
return "utf-8-sig"
elif data.startswith(b"\xFF\xFE") or data.startswith(b"\xFE\xFF"):
return "utf-16"
else: # in Windows, guessing utf-8 doesn't work, so we have to try
try:
with io.open(csv_file, encoding="utf-8") as f:
preview = f.read(222222)
return "utf-8"
except:
return locale.getdefaultlocale()[1]
and then
进而
file1 = input("Enter the name of the first file: ")
file1_open = open(file1, encoding=guess_encoding(file1))
file1_content = file1_open.read()
回答by Алексей Семенихин
It seems that you have problems with characters "\" and "/". If you use them in input - try to change one to another...
您似乎对字符“\”和“/”有问题。如果您在输入中使用它们 - 尝试将它们更改为另一个...