[Solved] How to print word that you want inside a textfile?

Use readlines and output the lines according to their index. with open(‘addon/entertainment.txt’) as f: lines = f.readlines() for line in lines: print(“{}. {}”.format(lines.index(line) + 1, line)) desired_lines = [3, 5] output = [] for desired in desired_lines: output.append(lines[desired – 1]) for o in output: print(“{}. {}”.format(output.index(o) + 1, o)) Alternatively, if you want to select … Read more

[Solved] Read a path from variable [closed]

If you use input() your example works. If you have a string variable like your example and you want to “turn it” to a raw string-like string (double escapes, in your case), you can use str.encode() path.encode(‘unicode_escape’) converts “C:\path” to “C:\\path” 1 solved Read a path from variable [closed]

[Solved] Python – OS Module ‘TypeError: ‘bool’ object is not iterable” [closed]

path.exists() just returns True or False depending on if that path exists. You should first check its existence via exists(), then use os.listdir() or glob.glob() to actually get a list of items in that path, but only if exists() returned True. solved Python – OS Module ‘TypeError: ‘bool’ object is not iterable” [closed]

[Solved] Is is safe to use dictionary class as switch case for python?

Yes the dictionary will be re-created if you leave and then re-enter that scope, for example def switch(a): responses = {1: ‘a’, 2: ‘b’, 3: ‘c’} print(id(responses)) return responses[a] Notice that the id of responses keeps changing which illustrates that a new object has been created each time >>> switch(1) 56143240 ‘a’ >>> switch(2) 56143112 … Read more

[Solved] Parsing date from fetched dataframe – Python

Here are some potential solutions you could use: import pandas as pd from yahooquery import Ticker from datetime import datetime, date aapl = Ticker(‘aapl’) df = aapl.history(period=’max’, interval=”1d”) df volume low high close open adjclose dividends splits 1980-12-12 14:30:00 117258400.0 0.513393 0.515625 0.513393 0.513393 0.405683 0.0 0.0 1980-12-15 14:30:00 43971200.0 0.486607 0.488839 0.486607 0.488839 0.384517 … Read more

[Solved] scipy.io.loadmat() is not returning dictionary [closed]

Why do you think this a loadmat object not having a dictionary? The error is at: def get_events(matlab_obj): … subjects = matlab_obj[“s”][0] … for subject_object in subjects: try: subject_hash = subject_object.my_hashedNumber[0][0] # AttributeError here matlab_obj[“s”] successfully accesses the loaded object as a dictionary. subjects is a numpy record array of shape (106,), and 58 fields. … Read more

[Solved] How to convert xml objects to python objects? [closed]

I’d go with using xmltodict: # -*- coding: utf-8 -*- import xmltodict data = “””<wordbook> <item> <name>engrossment</name> <phonetic><![CDATA[ɪn’grəʊsmənt]]></phonetic> <meaning><![CDATA[n. 正式缮写的文件,专注]]></meaning> </item> <item> <name>graffiti</name> <phonetic><![CDATA[ɡrəˈfi:ti:]]></phonetic> <meaning><![CDATA[n.在墙上的乱涂乱写(复数形式)]]></meaning> </item> <item> <name>pathology</name> <phonetic><![CDATA[pæˈθɔlədʒi:]]></phonetic> <meaning><![CDATA[n. 病理(学);〈比喻〉异常状态]]></meaning> </item> </wordbook>””” data = xmltodict.parse(data, encoding=’utf-8′) for item in data[‘wordbook’][‘item’]: print item[‘name’] prints: engrossment graffiti pathology You can also use BeautifulSoup or lxml – … Read more

[Solved] Python else condition prints several times

You are always printing something when you are testing, use a temporary variable and print the result after scanning the full list: success = False for i in sheet_data: if (i[0] == “BN”) and (i[1] == “YOU”): found_list.append(i) success = True if success: print(“Success”) else: print(“error”) solved Python else condition prints several times

[Solved] I want to group Columns in pandas [closed]

Try this: uniq_accounts = differenc[‘AccountID’].unique() grped = differenc.groupby(‘AccountID’) ## You can get the grouped data for a certain AccountId like this and store in a dictionary: d = {} for account in uniq_accounts: d[account] = grped.get_group(account) ##Now, dictionary has all accounts data in separate dataframes. You can access like this: for key in d.keys(): print(d[key]) … Read more

[Solved] How to compare string value list inside list.Python

This is a job for sets Convert your lists of lists to sets of tuples (you can’t have sets of lists, as sets can only contain hashable objects, which lists, as all built-in mutable objects, are not) a = set(map(tuple, [[‘abc’,’Hello World’],[‘bcd’,’Hello Python’]])) b = set(map(tuple, [[‘abc’,’Hello World’],[‘bcd’,’Hello World’],[‘abc’,’Python World’]])) or create them directly as … Read more