python儲存結果 python中如何儲存結果

2021-10-16 19:00:16 字數 542 閱讀 6067

一、建立檔案,儲存資料

1.使用python中內建的open函式,開啟txt檔案#mode 模式

#w 只能操作寫入  r 只能讀取 a 向檔案追加

#w+ 可讀可寫 r+可讀可寫  a+可讀可追加

#wb+寫入進製資料

#w模式開啟檔案,如果而檔案中有資料,再次寫入內容,會把原來的覆蓋掉

file_handle=open('1.txt',mode='w')

2.向檔案中寫入資料

2.1 write寫入#

換行符file_handle.write('hello word 你好

2.2writelines()函式   會將列表中的字串寫入檔案中,但不會自動換行,如果需要換行,手動新增換行符#引數 必須是乙個只存放字串的列表

file_handle.writelines(['hello

','world

','你好

','智遊

','鄭州

3.關閉檔案file_handle.close()

推薦學習《python教程》!

利用python將hive查詢結果儲存到mysql

python指令碼連線hive獲取返回值 此指令碼支援add jar file 使用者hive查詢結果的返回值更新mysql指定表指定字段 def mysqlexe sql conn mysqldb.connect host 10.10.111.111 user user passwd passwo...

json檔案內容 csv儲存檔案 Python

示例 import json import csv 從本地的json資料中讀取內容資訊,def writecsvdata ldata headers id name pin yin pin yin ad group id group code parent id device id chan no ...

pg如何儲存中間結果

在作查詢執行時,不知道中間結果如何儲存,無法知道每個欄位是什麼型別,尤其是作了sum,max等聚集函式後,返回的字段完全不在系統的屬性定義裡面,因此對於乙個中間結果,需要乙個描述這個tuple的資料結構.在pg裡面,一條tuple使用tupletableslot儲存,它每個欄位的資訊是由tupled...