pythonio文件數(shù)據(jù)的處理方法

這篇文章主要介紹“python io文件數(shù)據(jù)的處理方法”,在日常操作中,相信很多人在python io文件數(shù)據(jù)的處理方法問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”python io文件數(shù)據(jù)的處理方法”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

目前成都創(chuàng)新互聯(lián)已為近千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、鐵東網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

具體代碼如下:

格式有可能會(huì)亂,建議直接下載文件:

io讀取寫入

mac路徑path

path = '/Users/Public/odpscmd_public/odps_download/'

path = '/Users/Public'

csv 文檔

csv 文檔讀取

import pandas as pd

from datetime import datetime

import time

def read_csv(from_path):

    table = pd.read_csv(from_path,encoding='gbk') ##選取表

    return table

def write_csv(to_path,data):

    data.to_csv(to_path,index=False)

if __name__ == "__main__":

    start_time = time.time() # 開始時(shí)間

    path = '/Users/Public'

    path = '/Users/Public/odpscmd_public/odps_download/'

    file_name ='活動(dòng)三行為數(shù)據(jù).csv'

    from_path = path+"/"+file_name

    print(from_path)

    df = read_csv(from_path)

    df.head()

    end_time = time.time() #結(jié)束時(shí)間

    print("程序耗時(shí)%f秒.">

    print(df.head())

csv文檔保存

import re

import pandas  as pd

from datetime import datetime

import time

def write_csv(to_path,data):

    data.to_csv(to_path,index=False)

    return 'successful write'

if __name__ == "__main__":

    start_time = time.time() # 開始時(shí)間

    path = '/Users/Public'

    file_name ='新用戶首次注冊(cè)下單明細(xì)表.csv'

    to_path = path+"/"+file_name

    print(to_path)

    write_csv(to_path,df.head())

    end_time = time.time() #結(jié)束時(shí)間

    print("程序耗時(shí)%f秒." % (end_time - start_time))

excel文件IO

excel文件讀取

import re

import pandas  as pd

from datetime import datetime

import time

def read_xlsx(path,sheet_name):

    xlsx_file = pd.ExcelFile(path) ##路徑

    table = xlsx_file.parse(sheet_name) ##選取表

    return table

if __name__ == "__main__":

    start_time = time.time() # 開始時(shí)間

    path = '/Users/Public'

    file_name ='source雙子座uvpv.xlsx'

    sheet_name_list = {

    'hive':'Sheet',

    'MySQL':'Sheet4'

    }

    path = path+"/"+file_name

    sheet_name = sheet_name_list['hive']

    #sheet_name = sheet_name_list['email']    

    df = read_xlsx(path,sheet_name)

    print(df.head())

    end_time = time.time() #結(jié)束時(shí)間

    print("程序耗時(shí)%f秒." % (end_time - start_time))

Excel保存

def write_xlsx(to_path,data,sheet_name):

    writer = pd.ExcelWriter(to_path, engine='xlsxwriter')

    data.to_excel(writer,'Sheet1',index=False)

    writer.save()

if __name__ == "__main__":

    start_time = time.time() # 開始時(shí)間

    path = '/Users/Public'

    file_name ='撤點(diǎn)點(diǎn)位.xlsx'

    sheet_name = 'Sheet1'

    to_path = path+"\\"+file_name

    data = df

    try:

        write_xlsx(to_path,data,sheet_name)

    except:

        print('Excel保存失敗')

    end_time = time.time() #結(jié)束時(shí)間

    print("程序耗時(shí)%f秒." % (end_time - start_time))

mysql數(shù)據(jù)iO

mysql保存

import pymysql

from sqlalchemy import create_engine

#import mysql.connector as sql

def py_mysql(table,table_name):

    try:

        engine = create_engine("mysql+pymysql://root:password@127.0.0.1:3306/db_name?charset=utf8mb4")

        table.to_sql(name=table_name,con=engine,if_exists='replace',index=False,chunksize=10000

                    )

        print('數(shù)據(jù)庫(kù)寫入成功')

    except :

        print ('數(shù)據(jù)庫(kù)寫入失敗')

if __name__ == "__main__":

    start_time = time.time() # 開始時(shí)間

    try:

        py_mysql(df_trade_im,'tmp_news_im_v3')

    except:

        print('sql查詢失敗')

    end_time = time.time() #結(jié)束時(shí)間

    print("程序耗時(shí)%f秒." % (end_time - start_time))

mysql讀取

import pymysql

import time

import pandas as pd

def read_mysql(sql):

    db_connection= pymysql.connect(host="127.0.0.1",port=3306,user="root"

                                   ,passwd="password",db="db_name" )

    df = pd.read_sql(sql, con=db_connection)

    # 打開數(shù)據(jù)庫(kù)連接

    df = pd.read_sql(sql, con=db_connection)

    # 使用 cursor() 方法創(chuàng)建一個(gè)游標(biāo)對(duì)象 cursor

    #cursor = db.cursor()

    # 使用 execute() 方法執(zhí)行 SQL,如果表存在則刪除

    # 使用預(yù)處理語(yǔ)句創(chuàng)建表

    #data = cursor.execute(sql)

    # 關(guān)閉數(shù)據(jù)庫(kù)連接

    db_connection.close()

    return df

if __name__ == "__main__":

    start_time = time.time() # 開始時(shí)間

    sql ="""

    select * from table_name

    """

    try:

        df = read_mysql(sql)

        print(df.head())

    except:

        print('sql查詢失敗')

    end_time = time.time() #結(jié)束時(shí)間

    print("程序耗時(shí)%f秒." % (end_time - start_time))

到此,關(guān)于“python io文件數(shù)據(jù)的處理方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

文章題目:pythonio文件數(shù)據(jù)的處理方法
文章URL:http://bm7419.com/article6/geisog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站網(wǎng)站建設(shè)、網(wǎng)站改版商城網(wǎng)站、企業(yè)網(wǎng)站制作、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)