Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] csv output file in UTF-8 with BOM encoding

edited January 2014 in OpenSesame

Hi all,

Is it possible to save csv output file (the one, which saved with the logger) in UTF-8 with BOM encoding?
By default it is saved as an UTF-8 without BOM.

Thank you and best regards,
Boris.

Comments

  • edited 1:53AM

    Hi Boris,

    With a small hack you can do this, yes. If you place the following script in the prepare phase of an inline_script at the beginning of your experiment, you will have a logfile with utf_8_sig encoding, which I believe corresponds to UTF8 with BOM.

    # Close and re-open the logfile with a different codec.
    import codecs
    exp._log.close()
    exp._log = codecs.open(self.get('logfile'), 'w', encoding='utf_8_sig')
    

    You can use any standard codec, by the way, in case utf_8_sig is not the correct one:

    Just as a small aside, a BOM header is not necessary for UTF8 encoding. The reason is that UTF8 uses bytes as low-level units, instead of integers or some other unit that takes more than one byte. The byte order is therefore irrelevant, kind of like deciding whether you will read a single letter from left to right or right to left. But then again, you may have your reasons ;-)

    Cheers,
    Sebastiaan

  • edited 1:53AM

    Thanks a lot! :)

Sign In or Register to comment.