Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- 1) Создать текстовый txt-файл. COMPLETE
- 2) Вставить туда англоязычную статью из Википедии. COMPLETE
- 3) Написать функцию со следующим функционалом:
- 3.1) Прочитать файл построчно, вывести на печать. COMPLETE
- 3.2) Создать список и добавить туда непустые строки. COMPLETE
- 3.3) В строках оставить только латинские буквы и пробелы. Прочие символы удалить. COMPLETE
- 3.4) Объединить список в единую строку. вывести на печать. COMPLETE
- 3.5) Подсчитать количество вхождений различных слов в тескте. Подсчет вести в словаре. COMPLETE
- 3.6) Вывести на печать 10 наиболее популярных и наименее популярных слов (“ 1) -- hello -- 15”). COMPLETE
- 3.7) Заменить наименее популярные слова на “PYTHON”. COMPLETE
- 3.8) Создать новый txt-файл. COMPLETE
- 3.9) Записать текст в файл, разбивая на строки, при этом на каждой строке записывать COMPLETE
- не более 100 символов и не делить слова.
- """
- def wiki_function():
- f = open('wiki.txt', 'w')
- f.close()
- 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." #
- with open('wiki.txt', 'a') as f:
- f.write(some_data)
- print("Вот текст построчно:")
- with open('wiki.txt', 'r') as f:
- STRINGS = f.readlines()
- for i in STRINGS:
- print(' ------> ',i, '\n')
- with open('wiki.txt', 'r') as f:
- sentence = []
- non_void_strings = some_data.split('\n')
- sentence = some_data.split()
- print(sentence, '\n')
- print("".join(sentence), '\n')
- dct = {}
- for i in range(len(sentence)):
- if sentence[i] in dct:
- dct[sentence[i]] += 1
- else:
- dct.update({sentence[i]:1})
- print(dct, '\n')
- # 6
- for a in range(10):
- the_peremennaya = max(dct.values())
- for k, v in dct.items():
- if v == the_peremennaya:
- max_item = k
- print(a+1, ')', max_item, the_peremennaya)
- dct.pop(max_item, the_peremennaya)
- print(dct)
- with open('wiki.txt', 'r') as f:
- text = f.read()
- text = text.replace('\n', ' ')
- text = text.split(' ')
- print(text)
- for k,v in dct.items():
- for i in str(text):
- if i == k and v == 1:
- text = str(text)
- text = text.replace(i, 'PYTHON')
- text = list()
- text = ''.join(text)
- text1 = []
- with open('wiki.txt') as f:
- for line in f:
- text1.append(''.join(x for x in line.strip() if x.isalpha() or x.isspace()))
- text = ' '.join(text1)
- print('\nТекст с латинскими буквами и пробелами, без пустых строк и символов:')
- print(text)
- with open('wiki.txt', 'w') as f:
- f.write(text)
- f_text = []
- s = ''
- for word in text.split():
- if word in dct:
- word = 'PYTHON'
- if len(s + word) < 100:
- s = s + word
- else:
- f_text.append(s)
- s = word
- print(f_text)
- with open('wiki2.txt', 'w') as f:
- for i in f_text:
- f.write(i)
- f.write('\n')
- f.close
- # Вызов функции
- wiki_function()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement