[Solved] Counting occurrences of a word in a file

from pathlib import Path def count(filename: str, word = “hello”): file = Path(filename) text = file.read_text() lines_excluding_first = text.split(“\n”)[1:] counts = sum([sum(list(map(lambda y: 1 if word == y else 0, x.split(” “)))) for x in lines_excluding_first]) return counts Example: say you have a txt file like: sample.txt ———- this is the first line so this … Read more

[Solved] What is the python vesion of this?

Before getting into any of this: As chepner pointed out in a comment, this input looks like, and therefore is probably intended to be, JSON. Which means you shouldn’t be parsing it with regular expressions; just parse it as JSON: >>> s=””‘ {“text”: “Love this series!\ufeff”, “time”: “Hace 11 horas”, “author”: “HasRah”, “cid”: “UgyvXmvSiMjuDrOQn-l4AaABAg”}”’ >>> … Read more

[Solved] Read and print a user selected file’s contents [closed]

For what you are trying to do, a better method would be: import os def print_file_contents(file_path): assert os.path.exists(file_path), “File does not exist: {}”.format(file_path) with open(file_path) as f: print (f.read()) user_input = raw_input(“Enter a file path: “) # just input(…) in Python 3+ print_file_contents(user_input) solved Read and print a user selected file’s contents [closed]

[Solved] Why i don’t get the maximum recursion number in python?

The default recursion limit is 1000. >>> sys.getrecursionlimit() 1000 A program with a single recursive function will reach 999: start = 0 try: def recursion(): global start start += 1 recursion() recursion() except RecursionError: print(‘recursion :’, start, ‘times’) prints out recursion : 999 times Your program is creating a stack that has recursion(), then repeat(), … Read more

[Solved] python re.compile match dosn’t match backward slash in full path in windows [duplicate]

So there are two things: The assignment of s should be either with escaped back-slashes or as a raw string. I prefer the latter: s = r”C:\x\xxx\temp\downloads\test_dir\sql\my-ee.sql” you should use the search method instead of match to be content with a partial match. Then, you can use \\\\ in the regular expression, or – as … Read more

[Solved] It only return one letter

You’re returning early in the loop, you need to build up a string and return that. Either just return a joint string or build it up “”.join(chr(ord(s)+4) for s in pas) def cip(pas): ret_val = “” for i in pas: asci = ord(i) encryption = asci + 4 ret_val += chr(encryption) return ret_val solved It … Read more

[Solved] HOW TO REMOVE DUPLICATES IN A ROW IN PYTHON [closed]

To learn more about text processing in Python3 I recommend training on codingame.com. def removeDuplicates(inp): output =”” lastCharacter=”” for character in inp: output+=character*(character!=lastCharacter) lastCharacter=character return output inpTest =”AAABBCCDDAA” print(removeDuplicates(inpTest)) ABCDA 0 solved HOW TO REMOVE DUPLICATES IN A ROW IN PYTHON [closed]

[Solved] how to read multiple json values?

If you use the following code, response being your json: data = response[‘data’][‘students’] for student in data: print(‘{} {}:’.format(student[‘name’], student[‘lastname’])) for grade in student[‘grades’]: print(‘\t{} – {}’.format(grade[‘subject’], grade[‘score’])) This is what you’d get: Peter Henderson: math – A english – B Nick Simons: math – B english – C 0 solved how to read multiple … Read more