tags:

views:

67

answers:

2

I'm trying to use Python to concatenate a few javascript files together before minifying them, basically like so:

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

The minifier complains about illegal characters and syntax errors at the beginning of each file, so I did some diagnostics.

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

The first line of the file should, of course be "var $j = jQuery.noConflict(),"

In case it makes a difference, I'm working from within Windows.

Any thoughts?

Edit: Here's what I'm getting from the minifier:

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
+5  A: 

That's a UTF-8 BOM (Byte Order Mark). You've probably edited the file with Notepad.

Ned Batchelder
+3  A: 

EF BB BF is a Unicode Byte-Order Mark (BOM). Those are actually in your files. That's why Python is seeing it.

Either ignore/discard the BOM or reencode the files to omit it.

Borealid
You're entirely right. Digging through the YUIcompressor documentation, I found a mention to the --charset flag. Setting it to utf-8 solved my worries.
Chris