Unicode из python2 преобразовать в python3-код

Рейтинг: 0Ответов: 0Опубликовано: 20.02.2023

Помогите преобразовать 2 строчки кода if type(s) != unicode: fixed_tweets.append(unicode(s, errors="ignore"))(там где содержится unicode из python2 в формат для python3). Запутала окончательно в unicode

def get_tweets_predictions(tweets, perform_prints=True):
    fixed_tweets = []
    for i, t_orig in enumerate(tweets):
        s = t_orig
        try:
            s = s.encode("latin1")
        except:
            try:
                s = s.encode("utf-8")
            except:
                pass
        if type(s) != unicode:
            fixed_tweets.append(unicode(s, errors="ignore"))
        else:
            fixed_tweets.append(s)
    assert len(tweets) == len(fixed_tweets), "shouldn't remove any tweets"
    tweets = fixed_tweets
    print(len(tweets), " tweets to classify")

и в этой части кода тоже этот же unicode

select = SelectFromModel(LogisticRegression(class_weight='balanced', C=0.01, max_iter=15000))
final_features = select.get_support(indices=True)  # get indices of features
final_feature_list = [unicode(feature_names[i]) for i in
                      final_features]  # Get list of names corresponding to indices
print('final_feature_list= ', final_feature_list)

должно получиться: [u'america', u'american', u'anoth', u'ass', u'ass...]

пробовала создать функцию

def unicode(obj):
    return u'%s' % obj

но не получается того, что нужно. спасибо за любую помощь

Ответы

Ответов пока нет.