2020年3月10日 星期二

python to_txt

import pyodbc
import pandas as pd # 引用套件並縮寫為 pd

cnxn = pyodbc.connect("DRIVER={MySQL ODBC 8.0 Unicode Driver}; SERVER=localhost; PORT=3306;DATABASE=my_db; UID=root; PASSWORD=gimy0710;OPTION=3;CHARSET=UTF8;")
SQL = "SELECT * FROM expense where PAY_DATE > '2020/1/1' order by PAY_DATE desc"
file_name = "test.txt"

df = pd.read_sql(SQL, cnxn)
cnxn.close()

txt = ""
for row in df.columns:
    txt = txt + row + "|"
txt = txt + "\n"

for row in df.values:
    for i in range(0,len(row)):
        txt = txt + str(row[i]) + "|"
    txt = txt + "\n"
#print(txt)
f = open(file_name,'w')
f.write(txt)
f.close()

2020年2月26日 星期三

python to_csv

import pyodbc
import pandas as pd # 引用套件並縮寫為 pd

cnxn = pyodbc.connect("DRIVER={MySQL ODBC 8.0 Unicode Driver}; SERVER=localhost; PORT=3306;DATABASE=my_db; UID=root; PASSWORD=xxxxx;OPTION=3;CHARSET=UTF8;")
SQL = "SELECT * FROM expense where PAY_DATE > '2020/1/1' order by PAY_DATE desc"
df = pd.read_sql(SQL, cnxn)
cnxn.close()
df.to_csv('Result.csv')


###############################
import pandas as pd
df = pd.read_csv("C:/data/data.csv")
df.to_csv("C:/data/data_output.csv")

2020年2月22日 星期六

highcharts

<!DOCTYPE html>
<html>
<head>

<style>
.highcharts-figure, .highcharts-data-table table {
    min-width: 360px;
    max-width: 800px;
    margin: 1em auto;
}

.highcharts-data-table table {
font-family: Verdana, sans-serif;
border-collapse: collapse;
border: 1px solid #EBEBEB;
margin: 10px auto;
text-align: center;
width: 100%;
max-width: 500px;
}
.highcharts-data-table caption {
    padding: 1em 0;
    font-size: 1.2em;
    color: #555;
}
.highcharts-data-table th {
font-weight: 600;
    padding: 0.5em;
}
.highcharts-data-table td, .highcharts-data-table th, .highcharts-data-table caption {
    padding: 0.5em;
}
.highcharts-data-table thead tr, .highcharts-data-table tr:nth-child(even) {
    background: #f8f8f8;
}
.highcharts-data-table tr:hover {
    background: #f1f7ff;
}
</style>


</head>
<body>

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>

<figure class="highcharts-figure">
    <div id="container"></div>
    <p class="highcharts-description">
        Basic line chart showing trends in a dataset. This chart includes the
        <code>series-label</code> module, which adds a label to each line for
        enhanced readability.
    </p>
</figure>


<script>

var data_name = ['data0','data1'];
var data0 = [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];
var data1 = [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4];

var ratio = new Array(data0.length);
for(i=0;i < data0.length;i++){
ratio[i] = data0[i] / (data0[i] + data1[i]);
ratio[i] = ratio[i] * 100;
}


Highcharts.chart('container', {
    chart: {
        zoomType: 'xy'
    },
    title: {
        text: 'Average Monthly Temperature and Rainfall in Tokyo'
    },
    subtitle: {
        text: 'Source: WorldClimate.com'
    },
    xAxis: [{
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
            'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        crosshair: true
    }],
    yAxis: [{ // Primary yAxis
        labels: {
            format: '{value}°C',
            style: {
                color: Highcharts.getOptions().colors[1]
            }
        },
        title: {
            text: 'Temperature',
            style: {
                color: Highcharts.getOptions().colors[1]
            }
        }
    }, { // Secondary yAxis
        title: {
            text: 'Ratio (%)',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        },
        labels: {
            format: '{value} mm',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        },
        opposite: true
    }],
    tooltip: {
        shared: true
    },
    legend: {
        layout: 'vertical',
        align: 'left',
        x: 120,
        verticalAlign: 'top',
        y: 100,
        floating: true,
        backgroundColor:
            Highcharts.defaultOptions.legend.backgroundColor || // theme
            'rgba(255,255,255,0.25)'
    },

    plotOptions: {
        column: {
            stacking: 'normal',
            dataLabels: {
                enabled: true
            }
        }
    },

    series: [
{
        name: data_name[0],
        type: 'column',
       
        data: data0

    },
{
        name: data_name[1],
        type: 'column',
        data: data1

    },
{
        name: 'Ratio',
        type: 'line',
yAxis: 1,
        data: ratio
    }]
});
</script>

</body>
</html>

2020年2月11日 星期二

load excel

import pandas as pd
df = pd.read_excel (r'D:\Python_Test.xlsx')
#for an earlier version of Excel, you may need to use the file extension of 'xls'
lines = df.values
cols = df.columns
for i in cols:
     print(i)
for i in lines:
     print(i)

requests

import requests
url = "https://www.books.com.tw/"
htmlfile = requests.get(url)
print(type(htmlfile))
print(htmlfile.text)