[Solved] How to parse a file matching all the lines for a string and keep track of count?

Open the file and read all lines with open(“file.txt”) as f: lines = f.read().splitlines() Make a list of lines that contain the word “Error” data = [ line.lstrip(‘<‘).rstrip(‘>’).split(‘><‘) for line in lines if ‘Error’ in line ] Get the time and message items from the data list errors = [ { ‘time’ : line[0], ‘message’: … Read more

[Solved] Sort highest to lowest without built in [closed]

I dunno why one would do it without built-in functions, but here’s a working bubble sort example. http://rosettacode.org/wiki/Sorting_algorithms/Bubble_sort#Python def bubble_sort(seq): “””Inefficiently sort the mutable sequence (list) in place. seq MUST BE A MUTABLE SEQUENCE. As with list.sort() and random.shuffle this does NOT return “”” changed = True while changed: changed = False for i in … Read more

[Solved] I have an array of strings. I want to print those that start with the word New.

input_arr = [ “New: Hello”, “How are”, “you”, “New: I am”, “fine” ] def merge_messages(input_arr): delimiter = “New” return [delimiter+x for x in ” “.join(input_arr).split(delimiter) if x] print(merge_messages(input_arr)) However, I would highly recommend you take the advice given to you in the comments and read up on Python Strings. You can also view the Python … Read more

[Solved] Java to python conversion [closed]

If you are iterating over the items in some container, just iterate over that container; don’t use the index unless you need to. Right: slots = [1, 2, 3, 7] for slot in slots: print cards[slot] Wrong (or at least “writing C/C++/Java/C#/whatever in Python”): slots = [1, 2, 3, 7] for i in range(len(slots)): print … Read more

[Solved] How can I remove dollar signs ‘$’ from a list in Python?

Strip from the left with str.lstrip(): >>> sales = [‘$1.21’, ‘$2.29’, ‘$14.52’, ‘$6.13’, ‘$24.36’, ‘$33.85’, ‘$1.92’] >>> [s.lstrip(“$”) for s in sales] [‘1.21’, ‘2.29’, ‘14.52’, ‘6.13’, ‘24.36’, ‘33.85’, ‘1.92’] 1 solved How can I remove dollar signs ‘$’ from a list in Python?

[Solved] how to evaluate an equation with python [closed]

You can loop over values like this: for x in [-60.0, -45.0, -30.0]: # etc; notice how the .0 specifies a float print(‘D({0}) = {1}’.format(x, A*math.cos(x)**2+B*math.sin(x)+C)) If you intend for the output to be machine-readable, maybe change the format string to something like ‘{0},{1}’ for simple CSV output. Just print will print nothing (well, or … Read more

[Solved] Search and output with Python [closed]

I don’t think I fully understand your question. Posting your code and an example file would have been very helpful. This code will count all entries in all files, then it will identify unique entries per file. After that, it will count each entry’s occurrence in each file. Then, it will select only entries that … Read more