[PYTHON] Converts numbers with commas and triangles to numeric types.

1 This article is

I will post the code to convert the numerical value with △ and, described in the securities report to the numerical type that python can handle.

2 Code creation example

pattern 1

In the case of'△ 45,931'

test.py


moji='△45,931'

def conv(moji):
    moji=moji.replace('△', '-')
    moji=moji.split(' ')[-1]
    moji=moji.replace(',', '') 
    moji=int(moji)
    return moji

moji=conv(moji)
print(type(moji))  ##<class 'int'>
print(moji)        ##-45931

Pattern 2

In the case of'* 1 889,341'

test.py


moji='※1 889,341'
def conv(moji):
    moji=moji.replace('△', '-')
    moji=moji.split(' ')[-1]
    moji=moji.replace(',', '') 
    moji=int(moji)
    return moji

moji=conv(moji)
print(type(moji)) #<class 'int'>
print(moji) #889341

Recommended Posts

Converts numbers with commas and triangles to numeric types.
Sorting with mixed numbers and letters
Fractal to make and play with Python
Distinguish between numbers and letters with regular expressions
Scraping tabelog with python and outputting to CSV
Learn to recognize handwritten numbers (MNIST) with Caffe
Ten Puzzle-Make 10 with only 4 numbers and 4 arithmetic operations
How to create random numbers with NumPy's random module