gpt4 book ai didi

Python file.read() 在文件开头看到垃圾字符

转载 作者:太空狗 更新时间:2023-10-30 02:06:27 25 4
gpt4 key购买 nike

我正在尝试使用 Python 将几个 javascript 文件连接在一起,然后再缩小它们,基本上是这样的:

outfile = open("output.js", "w")
for somefile in a_list_of_file_names:
js = open(somefile)
outfile.write(js.read())
js.close()
outfile.close()

缩小器在每个文件的开头提示非法字符和语法错误,所以我做了一些诊断。

>>> r = open("output.js")
>>> somestring = r.readline()
>>> somestring
'\xef\xbb\xbfvar $j = jQuery.noConflict(),\n'
>>> print somestring
var $j = jQuery.noConflict(),

文件的第一行当然应该是“var $j = jQuery.noConflict(),”

如果它有所作为,我在 Windows 中工作。

有什么想法吗?

编辑:这是我从缩小器中得到的:

U:\>java -jar c:\path\yuicompressor-2.4.2.jar c:\path\somefile.js -o c:\path\bccsminified.js --type js -v

[INFO] Using charset Cp1252

[ERROR] 1:2:illegal character

[ERROR] 1:2:syntax error

[ERROR] 1:3:illegal character

最佳答案

这是一个 UTF-8 BOM(字节顺序标记)。您可能已经使用记事本编辑了该文件。

关于Python file.read() 在文件开头看到垃圾字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3469200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com