[Solved] Python – Read string from log file

Can be extracted with a simple regular expression, as long as the text file is in the same format as the Python log output you posted (Returns all of them): import re file=””.join([i for i in open(“yourfileinthesamefolder.txt”)]) serials=re.findall(“u’serial_number’: u'(.+)'”,file) print(serials) I suggest reading up on how to use regular expressions in Python: Regular Expression HOWTO … Read more

[Solved] a=’01101′ this has to be converted to [‘0′,’1′,’1′,’0′,’1’] ?? IN PYTHON [duplicate]

There is a function called list to do this directly.This can convert any string into list of characters. list(‘01101’) will return [‘0’, ‘1’, ‘1’, ‘0’, ‘1’] One more way is a=”01101″ a_list=[] for item in a: a_list.append(item) print(a_list) 5 solved a=’01101′ this has to be converted to [‘0′,’1′,’1′,’0′,’1’] ?? IN PYTHON [duplicate]

[Solved] How to make an on/off switch for a function in a python program?

Take a look at this example: from tkinter import * root = Tk() def run(): global rep if var.get() == 1: print(‘Hey’) rep = root.after(1000,run) #run the function every 2 second, if checked. else: root.after_cancel(rep) #cancel if the checkbutton is unchecked. def step(): print(‘This is being printed in between the other loop’) var = IntVar() … Read more

[Solved] How to fix the error “index out of range”

Your error occurs if you acces a list/string behind its data. You are removing things and access for i in range(len(data)): … data[i] += “,” + data[i + 1] If i ranges from 0 to len(data)-1 and you access data[i+1] you are outside of your data on your last i! Do not ever modify something … Read more

[Solved] Code a small Python dictionary

Read user input. Repeat for that many number of times – get items from dictionary using get attribute which handles KeyError itself: dic = {‘Hello’: ‘Salam’, ‘Goodbye’: ‘Khodafez’, ‘Say’: ‘Goftan’, ‘We’: ‘Ma’, ‘You’: ‘Shoma’} n = int(input()) for _ in range(n): print(dic.get(input(), ‘Wrong Input’)) EDIT: dic = {‘Hello’: ‘Salam’, ‘Goodbye’: ‘Khodafez’, ‘Say’: ‘Goftan’, ‘We’: ‘Ma’, … Read more

[Solved] Python arrays in numpy

Perhaps the closest thing in Python to this Javascript array behavior is a dictionary. It’s a hashed mapping. defaultdict is a dictionary that implements a default value. In [1]: from collections import defaultdict In [2]: arr = defaultdict(bool) Insert a couple of True elements: In [3]: arr[10] = True In [4]: arr Out[4]: defaultdict(bool, {10: … Read more

[Solved] How to get a link with web scraping

In the future, provide some code to show what you have attempted. I have expanded on Fabix answer. The following code gets the Youtube link, song name, and artist for all 20 pages on the source website. from bs4 import BeautifulSoup import requests master_url=”https://www.last.fm/tag/rock/tracks?page={}” headers = { “User-Agent”: “Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like … Read more

[Solved] Count value in dictionary

Keeping it simple, you could do something like this: reading = 0 notReading = 0 for book in bookLogger: if book[‘Process’] == ‘Reading’: reading += 1 elif book[‘Process’] == ‘Not Reading’: notReading += 1 print(f’Reading: {reading}’) print(f’Not Reading: {notReading}’) Alternatively, you could also use python’s list comprehension: reading = sum(1 for book in bookLogger if … Read more