User:Poornima7/Outreachy 2
Jump to navigation
Jump to search
Outreachy Task II[edit]
getting familiar with pywikibot
Below is the Python3 code for retrieving all the data from Wikidata Sandbox, printing it in the terminal, and then as a part of analysing the data, I've used the items() function for dictionary to find the count of each word present in the document.
{{{1}}}
import pywikibot from pywikibot import pagegenerators from pywikibot.data import api import requests
site = pywikibot.Site('en', 'wikipedia') repo = site.data_repository() page = pywikibot.Page(repo, "Q4115189") text = page.text
# list of the given words words = text.split() print(words) print('\n\n')
# creating a dictionary for counting the number of each word d = dict() for word in words : d[word] = d.get(word, 0) + 1
print("Count of each word is : \n") # looping through the key-value pairs of the dictionary 'd' for k, v in d.items() : print(k, v)
- To insert data at the end of the page, after the above code follow-up with this piece of code--
item = page.get() print("\n") item = item + "\n SOME NEW TEXT!" page.text = item try : page.save("WORKING!") except : print("THAT DID NOT WORK!") print(item) # prints the entire dictionary item with the additional text.