源代码字符串不能包含空字节 [英] Source code string cannot contain null bytes

查看:110
本文介绍了源代码字符串不能包含空字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有 676 行程序,现在我在打开文件时遇到了一个问题,我所有的代码都消失了,所以这次我做了一个备份.但是,当我尝试复制并粘贴我的备份代码然后运行它时,它给了我这个语法错误:

So i had 676 line program, Now i have had a issue in the past when I opened the file all my code had disapeared so this time i made a backup. However when I try to copy and paste my backedup code then run it, it gives me this syntax error:

Source code string cannot contain null bytes

当我第一次进行备份并且程序运行良好时没有显示.我真的不想通过 676 行重做这一切.说到 Python,我是一个非常基本的程序员.

This was not displayed when I first made the backup and the program was working fine. I really do not want to go through 676 lines redoing it all. Im a pretty basic programmer when it comes to Python.

我已经尝试删除所有空白和注释/#(红色背景色位于#的末尾)已经查看了所有代码,确实没有错误.

I already tried removing all white space and comments/# (Red background colour is at the end of a #) Already looked through all the code and it really has no errors.

推荐答案

问题可能正是错误消息告诉您的:您的备份副本不知何故感染"了一个或多个空字节(ASCII 值 00).一次粘贴你的代码一个块——比如 50 行——以找出哪些包含非法字节.删除最近添加的代码,一次可能是 5 行,以查找具有空字节的代码.重新输入有问题的行,然后继续下一行.

The problem is likely just what the error message tells you: your back-up copy somehow got "infected" with one or more null bytes (ASCII value 00). Paste your code one block at a time -- say, 50 lines -- to find which contain illegal bytes. Delete the most recently-added code, maybe 5 lines at a time, to find which has the null byte. Retype the offending line, and go on to the next.

另一种可能性是编写一个简单的 Python 脚本来读取文件并使用字符串 replace 方法删除空字节:

Another possibility is to write a simple Python script that reads the file and removes the null bytes use the string replace method:

with open("homework.py", 'r') as infile:
    hw = infile.readlines().replace(chr(0), '')

现在关闭文件,为'w'再次打开它,并将hw变量转储到它.

Now close the file, open it again for 'w', and dump the hw variable to it.

这篇关于源代码字符串不能包含空字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆