M
M
Maxim Siomin2020-06-30 20:52:46
Python
Maxim Siomin, 2020-06-30 20:52:46

How to work with cyrillic in python?

Here in this program:

spoiler
with open('authors.txt', 'r') as file:
    authorsList = file.read().splitlines()

with open('names.txt', 'r') as file:
    namesList = file.read().splitlines()

booksList = []
namesList2 = []
authorsList2 = []

x = 0
for book in namesList:
    x += 1
    stripData = f'{x}. '
    appendData = book.strip(stripData)
    namesList2.append(appendData)


for x in range(0, len(authorsList)):
    booksList.append(f' {authorsList[x]}, {namesList2[x]}')




def showAllBooks():
    for book in booksList:
        print(book)


def searchBook():
    global bookAppears
    
    print('Type a book\'s name')
    print('Введите название книги')
    print()
    print()
    searchingText = input()

    x = 1
    print()
    print()
    for book in booksList:
        if searchingText in book:
            print(f' {x}. {book}')
            x += 1
            bookAppears = True

    if bookAppears == False:
        print('Such book is not found')
        print('Такая книга не найдена')








def start():
    print('Starting...')
    print('Запуск...')
    print()
    print('Type 1 if you want to view all books.')
    print('Type 2 if you want to search a book.')
    print()
    print('Введите 1, если Вы хотите просмотреть все книги.')
    print('Введите 2, если Вы хотите найти книгу.')
    print()
    message = input()
    print()
    
    if message == '1':
        showAllBooks()
        
    elif message == '2':
        searchBook()
        
    else:
        print('Error')
        print('Ошибка')
        

def main():
    start()


bookAppears = False

main()
input()

If the txt files contain books in Latin, then everything is ok. If in Cyrillic, then nothing works.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-06-30
@MaxSiominDev

So it won't read?

with open('authors.txt', 'r', encoding='utf-8') as file:

D
Dr. Bacon, 2020-06-30
@bacon

Probably first you need to find out in what "Russian" encoding they are written? And then use encoding at https://docs.python.org/3/library/functions.html#open

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question