Test

Last updated: April 9th, 20202020-04-09Project preview
In [3]:
import pandas
import os
from yandex_translate import YandexTranslate, YandexTranslateException


def get_translated_column(
    yandex_token: str,
    table: pandas.DataFrame,
    column_name: str,
    lang_to_from: str = 'ru-en',
):
    translator = YandexTranslate(yandex_token)
    return table[column_name].apply(lambda x: translator.translate(x, lang=lang_to_from)['text'][0])


def translate_column(
    yandex_token_list: list,
    file_path: str,
    original_column_name: str,
    translated_column_name: str,
    save_to_file_path: str = None,
    lang_to_from: str = 'ru-en',
    start_row: int = 0,
    finish_row: int = None,
    batch: int = 10,
):
    # получить таблицу из файла
    ext = os.path.splitext(file_path)[1]
    if ext == ".csv":
        table = pandas.read_csv(file_path)
    elif ext in [".xlsx", ".xls"]:
        table = pandas.read_excel(file_path)
    else:
        raise ValueError(f"Неподдерживаемый формат: {ext}")
    
    if not translated_column_name in table.columns:
        table[translated_column_name] = pandas.Series([], dtype=str)
    if not finish_row:
        finish_row = len(table)
    
    yandex_token_pos = 0

    for i in range(((finish_row - start_row) // batch) + 1):
        l = start_row + i * batch
        r = start_row + (i + 1) * batch
        print(f"Перевод строк {l}-{r}.")
        try:
            translated_column = get_translated_column(
                yandex_token=yandex_token_list[yandex_token_pos],
                table=table[l:r],
                column_name=original_column_name,
                lang_to_from=lang_to_from,
            )
        except YandexTranslateException:
            yandex_token_pos += 1
            if yandex_token_pos >= len(yandex_token_list):
                raise YandexTranslateException(
                    "Лимиты на перевод достигнуты для всех переданных токенов."
                )
            translated_column = get_translated_column(
                yandex_token=yandex_token_list[yandex_token_pos],
                table=table[l:r],
                column_name=original_column_name,
                lang_to_from=lang_to_from,
            )
                
        table[translated_column_name][l:r] = translated_column
        print(f"Перевод строк {l}-{r} сохранен.")
        
        # сохранить таблицу с переведенным столбцом
        if not save_to_file_path:
            save_to_file_path = file_path
        table.to_csv(save_to_file_path, index=False)
    
    return table
In [4]:
# токен можно получить здесь: https://translate.yandex.ru/developers/keys
yandex_token_list = [
    'trnsl.1.1.20200331T110242Z.9b513b0ed4f09237.33aec5c62db6c59d166b2d087085a48984c5e773',  # ivan
    'trnsl.1.1.20190906T170533Z.2cdef571e4680794.3161998ff0a03702bffae1c9b1c6f5cfbd84a682',  # kamil@zonesmart.ru
    'trnsl.1.1.20200110T122307Z.0eab94a550695b16.851da33dae66e111a419c298ea04bbc64e9889cd',  # dev@zonesmart.ru
    'trnsl.1.1.20200331T111658Z.debed69e977278e3.dc2212dc94324502e515153f8f706e70bfe9f323',  # info@zonesmart.ru
]


# перевод названий товаров c русского на английский
translate_column(
    yandex_token_list=yandex_token_list,
    file_path='litva.csv',
    save_to_file_path='litva.csv',
    original_column_name='SEO name',
    translated_column_name='SEO name (test eng)',
    lang_to_from='ru-en',
    start_row=0,
    finish_row=30,
)
Перевод строк 0-10.
<ipython-input-3-eec039db88de>:67: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  table[translated_column_name][l:r] = translated_column
Перевод строк 0-10 сохранен.
Перевод строк 10-20.
Перевод строк 10-20 сохранен.
Перевод строк 20-30.
Перевод строк 20-30 сохранен.
Перевод строк 30-40.
Перевод строк 30-40 сохранен.
Out[4]:
SKU Title Product code Sales price Price before promotion Available quantity SEO name Meta description Short description Full description ... Manufacturer Related products UPC (barcode) Country of origin Status Title (eng) Full description (eng) Short description (eng) SEO name (eng) SEO name (test eng)
0 69284 Triumph žalsva braletė su baltais neriniais Ai... Airy-sensation-P 25 NaN 1 Triumph žalsva braletė su baltais neriniais Ai... Triumph bralete &lt;p&gt;Pristatymas 1-2 d.d.&lt;/p&gt; &lt;p&gt;Triumph žalsva prailginta braletė su ... ... NaN 58266, 59602, 59603, 59604, 59605, 59606, 5960... NaN NaN 1 Triumph green braletė with white neriniais Air... &lt;p&gt;Triumph greenish lengthening braletė ... &lt;p&gt;Delivery in 1-2 d.d.&lt;/p&gt; Triumph green braletė with white neriniais Air... Triumph žalsva braletė su baltais neriniais Ai...
1 74080 Triumph šviesiai žalsvos spalvos 38 dydžio kel... Essential-Minimmizer-Hipster-lgreen 14,95 NaN 1 Triumph šviesiai žalsvos spalvos 38 dydžio kel... Triumph kelnaitės &lt;p&gt;Pristatymas 1-2 d.d.&lt;/p&gt; &lt;p&gt;Triumph &amp;scaron;viesiai žalsvos s... ... NaN 74079 NaN NaN 1 Triumph light greenish color 38 size panties E... &lt;p&gt;Triumph &amp;scaron;viesiai greenish ... &lt;p&gt;Delivery in 1-2 d.d.&lt;/p&gt; Triumph light greenish color 38 size panties E... Triumph šviesiai žalsvos spalvos 38 dydžio kel...
2 74060 Triumph šviesiai rožinė 75B liemenėlė Lovely A... Lovely-Angel-Curves-WHP-pink 37,95 NaN 1 Triumph šviesiai rožinė 75B liemenėlė Lovely A... Triumph liemenėlė &lt;p&gt;Pristatymas 1-2 d.d.&lt;/p&gt; &lt;p&gt;Triumph &amp;scaron;viesiai rožinė 75... ... NaN NaN NaN NaN 1 Triumph light pink 75B bra Lovely Angel Curves... &lt;p&gt;Triumph &amp;scaron;viesiai pink 75B ... &lt;p&gt;Delivery in 1-2 d.d.&lt;/p&gt; Triumph light pink 75B bra Lovely Angel Curves... Triumph šviesiai rožinė 75B liemenėlė Lovely A...
3 73928 Triumph violetinė marga sportinė liemenėlė Tri... Triaction-magic-motion-pro-MWP 34,95 NaN 13 Triumph violetinė marga liemenėlė Triaction Ma... sportinė liemenėlė &lt;p&gt;Pristatome per 1-2 d.d.&lt;/p&gt; &lt;p&gt;Triumph&amp;nbsp;Triaction Magic Moti... ... NaN 10, 15, 16, 26, 27, 12831, 12832, 12833, 12835... NaN NaN 1 Triumph purple varied sports bra Triaction Mag... &lt;p&gt;Triumph&amp;nbsp;Triaction Magic Moti... &lt;p&gt;we Deliver within 1-2 d.d.&lt;/p&gt; Triumph purple varied in bra Triaction Magic M... Triumph Triaction violetinė marga liemenėlė Ma...
4 73721 Triumph violetiniai su žalia ir melyna spalva ... Translucent-Essence-Mini-E 24,95 NaN 1 Triumph violetiniai su žalia ir melyna spalva ... Triumph kelnaitės &lt;p&gt;Pristatymas 1-2 d.d.&lt;/p&gt; &lt;p&gt;Triumph violetiniai su žalia ir melyn... ... NaN NaN NaN NaN 1 Triumph violets with the green and blue color ... &lt;p&gt;Triumph violets with the green and bl... &lt;p&gt;Delivery in 1-2 d.d.&lt;/p&gt; Triumph violets with the green and blue color ... Triumph violetiniai su žalia ir melyna spalva ...
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
344 73982 Triumph mėlynos spalvos 38 dydžio kelnaitės Mo... Modern-Finesse-Tai 24,95 NaN 1 Triumph mėlynos spalvos 38 dydžio kelnaitės Mo... Triumph kelnaitės &lt;p&gt;Pristatymas 1-2 d.d.&lt;/p&gt; &lt;p&gt;Triumph &amp;nbsp;mėlynos spalvos 38 ... ... NaN 13488, 58202, 58248, 59584, 59588, 59591, 5959... NaN NaN 1 Triumph blue 38 size panties Modern Finesse It &lt;p&gt;Triumph &amp;nbsp;blue in colour 38 s... &lt;p&gt;Delivery in 1-2 d.d.&lt;/p&gt; Triumph blue 38 size panties Modern Finesse It... NaN
345 73712 Triumph baltos spalvos liemenėlė Yselle N Yselle-N-white 34,95 NaN 7 Triumph baltos spalvos liemenėlė Yselle N - ww... Triumph apatiniai &lt;p&gt;Pristatymas kurjeriu 1-2 d.d.&lt;/p&gt; &lt;p&gt;Triumph &amp;nbsp;baltos&amp;nbsp; sp... ... NaN 981, 2551, 2558, 2860, 2888, 54298, 58196, 582... NaN NaN 1 Triumph white bra Yselle N &lt;p&gt;Triumph &amp;nbsp;white&amp;nbsp; col... &lt;p&gt;Delivery kurjeriu 1-2 d.d.&lt;/p&gt; Triumph white bra Yselle N - www.apatinukai.lt... NaN
346 73937 Sloggi tamsiai melynos spalvos triko su liemen... S-Serenity-Body 37 NaN 5 Sloggi tamsiai melynos spalvos triko su liemen... Sloggi formojantis triko su liemenėlė &lt;p&gt;Pristatymas 1-2 d.d.&lt;/p&gt; &lt;p&gt;Sloggi tamsiai melynos spalvos&amp;nb... ... NaN NaN NaN NaN 1 Sloggi dark melynos-colored tights with liemen... &lt;p&gt;Sloggi dark melynos color&amp;nbsp; t... &lt;p&gt;Delivery in 1-2 d.d.&lt;/p&gt; Sloggi dark melynos-colored tights with liemen... NaN
347 73735 Sloggi melynas dviejų dalių maudymosi kostiumė... Wow-Comfort-Mellow-TANGA 43 NaN 1 Sloggi melynas dviejų dalių maudymosi kostiumė... Dviejų dalių maudymosi kostiumėlis &lt;p&gt;Sloggi melynas dviejų dalių&amp;nbsp;... &lt;p&gt;Sloggi melynas dviejų dalių&amp;nbsp;... ... NaN NaN NaN NaN 1 Sloggi melynas two piece swimsuit Wow Comfort ... &lt;p&gt;Sloggi melynas the two parts of the&a... &lt;p&gt;Sloggi melynas the two parts of the&a... Sloggi melynas two piece swimsuit Wow Comfort ... NaN
348 73791 Sloggi Nightblue Pearls CTOW 44D/44 dviejų dal... Nightblue-Pearls-CTOW 34 NaN 1 Sloggi Nightblue Pearls CTOW 44D/44 dviejų dal... Sloggi Nightblue Pearls CTOW 44D/44 dviejų dal... &lt;p&gt;Pristatoma per 1-2 d.d.&lt;/p&gt; &lt;p&gt;Sloggi Nightblue Pearls CTOW 44D/44 d... ... NaN NaN NaN NaN 1 Sloggi Nightblue Pearls CTOW 44D/44 two parts ... &lt;p&gt;Sloggi Nightblue Pearls CTOW 44D/44 t... &lt;p&gt;Delivered within 1-2 d.d.&lt;/p&gt; Sloggi Nightblue Pearls CTOW 44D/44 two parts ... NaN

349 rows × 22 columns

In [ ]:
 
In [ ]:
 
In [ ]:
 
In [ ]:
 
Notebooks AI
Notebooks AI Profile20060