您好,登錄后才能下訂單哦!
這篇文章主要介紹“python io文件數據的處理方法”,在日常操作中,相信很多人在python io文件數據的處理方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”python io文件數據的處理方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
具體代碼如下:
格式有可能會亂,建議直接下載文件:
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() # 開始時間
path = '/Users/Public'
path = '/Users/Public/odpscmd_public/odps_download/'
file_name ='活動三行為數據.csv'
from_path = path+"/"+file_name
print(from_path)
df = read_csv(from_path)
df.head()
end_time = time.time() #結束時間
print("程序耗時%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() # 開始時間
path = '/Users/Public'
file_name ='新用戶首次注冊下單明細表.csv'
to_path = path+"/"+file_name
print(to_path)
write_csv(to_path,df.head())
end_time = time.time() #結束時間
print("程序耗時%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() # 開始時間
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() #結束時間
print("程序耗時%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() # 開始時間
path = '/Users/Public'
file_name ='撤點點位.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() #結束時間
print("程序耗時%f秒." % (end_time - start_time))
mysql數據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('數據庫寫入成功')
except :
print ('數據庫寫入失敗')
if __name__ == "__main__":
start_time = time.time() # 開始時間
try:
py_mysql(df_trade_im,'tmp_news_im_v3')
except:
print('sql查詢失敗')
end_time = time.time() #結束時間
print("程序耗時%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)
# 打開數據庫連接
df = pd.read_sql(sql, con=db_connection)
# 使用 cursor() 方法創建一個游標對象 cursor
#cursor = db.cursor()
# 使用 execute() 方法執行 SQL,如果表存在則刪除
# 使用預處理語句創建表
#data = cursor.execute(sql)
# 關閉數據庫連接
db_connection.close()
return df
if __name__ == "__main__":
start_time = time.time() # 開始時間
sql ="""
select * from table_name
"""
try:
df = read_mysql(sql)
print(df.head())
except:
print('sql查詢失敗')
end_time = time.time() #結束時間
print("程序耗時%f秒." % (end_time - start_time))
到此,關于“python io文件數據的處理方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。