It is required to parse vk by the given parameters and write the result to a file in Python 3.5.

 search_result = api.users.search(sort=sort_index, sex=sex_index, age_to=age_index, offset=offset_index, count=count_index, country=country) result = open('result.txt','a') while index != (count_index - offset_index): result.write(str(search_result[index + 1])) result.write('\n') index += 1 result.close() 

I get the error:

 Traceback (most recent call last): File "E:***\vkscript.py", line 19, in <module> result.write(str(search_result[index + 1])) File "C:\Python\lib\encodings\cp1251.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u039a' in position 16: character maps to <undefined> 

    2 answers 2

    We must try to specify the file encoding result = open('result.txt','a', encoding='utf-8')

    • I do in windows 10 does not help, how to be? - Alex
    • @Alex ask your question with a reproducible code sample and a specific description of what exactly "does not help" means. - Sergey Gornostaev

    Add an encoding parameter. Simply, the default encoding is cp1251 :

     open('result.txt', mode='a', encoding='utf8') 

    And to avoid having to manually call close , use the context manager, it will close itself:

     with open('result.txt', mode='a', encoding='utf8') as f: while index != (count_index - offset_index): f.write(str(search_result[index + 1])) f.write('\n') index += 1 
    • the entire cycle can probably be replaced by print(*search_result, sep='\n', file=f) . If you want to save only part of the results, then you can search_result[index+1:count_index - offset_index + 1] instead of search_result . - jfs
    • looks good on you) - gil9red