Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

Errore encoding apertura file

g
giacomo_venturini (6680 points)
3 6 40
in HW4 obbligatorio by (6.7k points)
closed by

L'esecuzione di tutti i test va in errore nel momento in cui apro i file utilizzando l'encoding 'utf-8-sig', mentre se utilizzo 'utf8' l'errore non si presenta, ma ovviamente l'encoding è sbagliato.

L'errore che ricevo è il seguente:

_______________________________________________________________________________________ Test.test_random_2592_00001_random_2592_rnd ________________________________________________________________________________________

self = <test_01.Test testMethod=test_random_2592_00001_random_2592_rnd>, filename = 'random-2592-rnd.txt', prosody = [0, 1, 0, 2, 3, 4, ...], module = 81, lengths = [27, 26, 27, 10, 10, 14, ...]
finals = ['vkvco', 'lfbqe', 'vkvco', 'ftfxe', 'hxlkoi', 'wcbueeio', ...]

    @file_data("random-2592-rnd.json")
    def test_random_2592(self, filename, prosody, module, lengths, finals):
>       return self.do_test(filename, prosody, module, lengths, finals)

test_01.py:114:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
test_01.py:34: in do_test
    result   = program.ex1(poetry_file)
program01.py:67: in ex1
    with open(poem_filename, encoding='UTF-8-SIG') as file:
testlib.py:66: in _check_open
    return self.__orig_open(*args, **kargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

encoding = 'utf-8-sig'

    def search_function(encoding):

        # Cache lookup
        entry = _cache.get(encoding, _unknown)
        if entry is not _unknown:
            return entry

        # Import the module:
        #
        # First try to find an alias for the normalized encoding
        # name and lookup the module using the aliased name, then try to
        # lookup the module using the standard import scheme, i.e. first
        # try in the encodings package, then at top-level.
        #
        norm_encoding = normalize_encoding(encoding)
        aliased_encoding = _aliases.get(norm_encoding) or \
                           _aliases.get(norm_encoding.replace('.', '_'))
        if aliased_encoding is not None:
            modnames = [aliased_encoding,
                        norm_encoding]
        else:
            modnames = [norm_encoding]
        for modname in modnames:
            if not modname or '.' in modname:
                continue
            try:
                # Import is absolute to prevent the possibly malicious import of a
                # module with side-effects that is not in the 'encodings' package.
>               mod = __import__('encodings.' + modname, fromlist=_import_tail,
                                 level=0)
E                                TypeError: _check_import() got an unexpected keyword argument 'fromlist'

F:\Anaconda3\lib\encodings\__init__.py:98: TypeError

848 views
closed with the note: usate encoding='utf8'

1 Answer

G
Giordano_Dionisi (3100 points)
16 41 59
by (3.1k points)
Perchè utilizzi utf-8-sig al posto di utf-8 ? Mi sembra che i caratteri si codifichino bene anche con utf-8, no ??
g
giacomo_venturini (6680 points)
3 6 40
by (6.7k points)

con utf-8 i caratteri non mi vengono recuperati correttamente, per esempio se apro il primo file con utf-8-sig la prima riga mi risulta:

Dì pestaggio tessessi allarmai, Partenopea!

mentre con utf-8 ottengo

Dì pestaggio tessessi allarmai, Partenopea!

G
Giordano_Dionisi (3100 points)
16 41 59
by (3.1k points)
Calcola io sto sotto Windows ed uso l'endoding='utf-8' e non mi dà minimamente questo problema, lo ho appena ritestato
g
giacomo_venturini (6680 points)
3 6 40
by (6.7k points)
edited by
Sembra che comunque usare utf-8 non infici il risultato.
Nonostante alcune lettere vengano modificate risulta che sia sempre da vocale a vocale la conversione, quindi i conti finali sono corretti

edit: omettendo di specificare l'encoding alcuni caratteri non vengono interpretati correttamente, e consegnando l'HW sembra che dichiarare l'encoding mandi in errore tutti i test