Advertisement
Toxotsist

Task B4

Apr 15th, 2021 (edited)
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.43 KB | None | 0 0
  1. """
  2. 1) Создать текстовый txt-файл.                                                                                          COMPLETE
  3. 2) Вставить туда англоязычную статью из Википедии.                                                                      COMPLETE
  4. 3) Написать функцию со следующим функционалом:
  5. 3.1) Прочитать файл построчно, вывести на печать.                                                                       COMPLETE
  6. 3.2) Создать список и добавить туда непустые строки.                                                                    COMPLETE
  7. 3.3) В строках оставить только латинские буквы и пробелы. Прочие символы удалить.                                       COMPLETE
  8. 3.4) Объединить список в единую строку. вывести на печать.                                                              COMPLETE
  9. 3.5) Подсчитать количество вхождений различных слов в тескте. Подсчет вести в словаре.                                  COMPLETE
  10. 3.6) Вывести на печать 10 наиболее популярных и наименее популярных слов (“ 1) -- hello -- 15”).                        COMPLETE
  11. 3.7) Заменить наименее популярные слова на “PYTHON”.                                                                    COMPLETE
  12. 3.8) Создать новый txt-файл.                                                                                            COMPLETE
  13. 3.9) Записать текст в файл, разбивая на строки, при этом на каждой строке записывать                                    COMPLETE
  14. не более 100 символов и не делить слова.
  15. """
  16.  
  17. def wiki_function():
  18.     f = open('wiki.txt', 'w')
  19.     f.close()
  20.     some_data = "Это статья из Википедии:\nPython is an interpreted, high-level and general-purpose programming language.\nPython's design philosophy emphasizes code readability with its notable use of significant indentation.\nIts language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects. Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented and functional programming.\nPython is often described as a batteries included language due to its comprehensive standard library.\nGuido van Rossum began working on Python in the late 1980s, as a successor to the ABC programming language, and first released it in 1991 as Python 0.9.0. Python 2.0 was released in 2000 and introduced new features, such as list comprehensions and a garbage collection system using reference counting and was discontinued with version 2.7.18 in 2020.\nPython 3.0 was released in 2008 and was a major revision of the language that is not completely backward-compatible and much Python 2 code does not run unmodified on Python 3." #
  21.     with open('wiki.txt', 'a') as f:
  22.         f.write(some_data)
  23.     print("Вот текст построчно:")
  24.     with open('wiki.txt', 'r') as f:
  25.         STRINGS = f.readlines()
  26.         for i in STRINGS:
  27.             print(' ------> ',i, '\n')
  28.     with open('wiki.txt', 'r') as f:
  29.         sentence = []
  30.         non_void_strings = some_data.split('\n')
  31.         sentence = some_data.split()
  32.         print(sentence, '\n')
  33.  
  34.     print("".join(sentence), '\n')
  35.     dct = {}
  36.     for i in range(len(sentence)):
  37.         if sentence[i] in dct:
  38.             dct[sentence[i]] += 1
  39.         else:
  40.             dct.update({sentence[i]:1})
  41.     print(dct, '\n')
  42.  
  43.     # 6
  44.     for a in range(10):
  45.         the_peremennaya = max(dct.values())
  46.         for k, v in dct.items():
  47.             if v == the_peremennaya:
  48.                 max_item = k
  49.         print(a+1, ')', max_item, the_peremennaya)
  50.         dct.pop(max_item, the_peremennaya)
  51.     print(dct)
  52.     with open('wiki.txt', 'r') as f:
  53.         text = f.read()
  54.     text = text.replace('\n', ' ')
  55.     text = text.split(' ')
  56.     print(text)
  57.     for k,v in dct.items():
  58.         for i in str(text):
  59.             if i == k and v == 1:
  60.                 text = str(text)
  61.                 text = text.replace(i, 'PYTHON')
  62.     text = list()
  63.     text = ''.join(text)
  64.     text1 = []
  65.     with open('wiki.txt') as f:
  66.         for line in f:
  67.             text1.append(''.join(x for x in line.strip() if x.isalpha() or x.isspace()))
  68.         text = ' '.join(text1)
  69.         print('\nТекст с латинскими буквами и пробелами, без пустых строк и символов:')
  70.         print(text)
  71.     with open('wiki.txt', 'w') as f:
  72.         f.write(text)
  73.     f_text = []
  74.     s = ''
  75.     for word in text.split():
  76.         if word in dct:
  77.             word = 'PYTHON'
  78.         if len(s + word) < 100:
  79.             s = s + word
  80.         else:
  81.             f_text.append(s)
  82.             s = word
  83.     print(f_text)
  84.     with open('wiki2.txt', 'w') as f:
  85.         for i in f_text:
  86.             f.write(i)
  87.             f.write('\n')
  88.     f.close
  89. # Вызов функции
  90. wiki_function()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement