I want to analyze logs with Python

A stockpile of functions used to analyze / aggregate the logs of each server and program. If you want to analyze logs with Python, please refer to it!

Separate one line with a space

Use split. The usage of split is roughly divided.

--Do not specify a delimiter like `str.split ()` --``` str.split ('delimiter') Specify a delimiter like `` `, and have the string str returned a list separated by the specified delimiter.

There are two uses for this. Use the former `str.split ()`. Logs are often separated by tabs or a single character of space. It is good to specify the delimiter if the delimiter is unified in the log, but unfortunately, both the tab and the space for one character may be confused.

str.split()If so, the tab and the space for one character will be separated as the same "space".


 Of course, when separated by commas, etc., use `` `str.split ('separator')` ``.

# Count
 When counting the number of accesses


#### **` count.py`**
```py

#Function to count
def count( targets):
    cnt = {}
    for target in targets:
        if target in cnt.keys():
            cnt[target] += 1
        else:
            cnt[target] = 1
    return cnt

#I want to find out the number of occurrences of a character string in this list...
targets = ['aaa','bbb','ccc','aaa','bbb','aaa']
#Call the function to count
result = count( targets)
#Viewing count results
for key in result.keys():
    print(key + ':' + result[key]) 

You can implement such a function and call it, but the `` `collections.Counter``` class that counts just by creating an instance in the Collections included in the standard Python3 package. There is, so let's use it.

If you rewrite the count.py written above using the `` `collections.Counter``` class, it will look like this.

count.py


import collections

#I want to find out the number of occurrences of a character string in this list...
targets = ['aaa','bbb','ccc','aaa','bbb','aaa']
#Call the function to count
result = collections.Counter( targets)
#Viewing count results
for key in result.keys():
    print(key + ':' + result[key]) 

The method of viewing the count results does not change. If so, it's better to use the `` `collections.Counter``` class, which can do other things as well as count!

Recommended Posts

I want to analyze logs with Python
I want to debug with Python
I want to play with aws with python
I want to be able to analyze data with Python (Part 3)
I want to be able to analyze data with Python (Part 1)
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
I want to use MATLAB feval with python
I want to analyze songs with Spotify API 2
I want to make a game with Python
I want to analyze songs with Spotify API 1
I want to use Temporary Directory with Python2
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to write to a file with Python
I want to handle optimization with python and cplex
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I want to AWS Lambda with Python on Mac!
[ML Ops] I want to do multi-project with Python
I tried to analyze J League data with Python
I want to run a quantum computer with Python
I want to do ○○ with Pandas
I want to specify another version of Python with pyvenv
I want to automatically attend online classes with Python + Selenium!
[Python] I want to use the -h option with argparse
I want to detect objects with OpenCV
I want to blog with Jupyter Notebook
I want to use jar from python
I want to build a Python environment
I want to pip install with PythonAnywhere
I wanted to solve ABC172 with Python
I want to use a wildcard that I want to shell with Python remove
I want to know the weather with LINE bot feat.Heroku + Python
I want to monitor UNIQLO + J page updates [Scraping with python]
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to output the beginning of the next month with Python
I want to do a full text search with elasticsearch + python
[Pandas] I tried to analyze sales data with Python [For beginners]
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I started to analyze
I want to do Dunnett's test in Python
I wanted to solve NOMURA Contest 2020 with Python
I want to memoize including Python keyword arguments
I want to create a window in Python
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to mock datetime.datetime.now () even with pytest!
I want to display multiple images with matplotlib.
I want to knock 100 data sciences with Colaboratory
I wanted to install Python 3.4.3 with Homebrew + pyenv
I want to be an OREMO with setParam!
I tried to output LLVM IR with Python
I want to merge nested dicts in Python
I tried to automate sushi making with python
I want to use ceres solver from python
I don't want to use -inf with np.log
I want to use ip vrf with SONiC
I want to start over with Django's Migrate
I want to sell Mercari by scraping python
[Python] I want to manage 7DaysToDie from Discord! 2/3