python之資料處理

2022-05-07 01:09:08 字數 3569 閱讀 1082

#

檔案資料讀寫的基本操作

import this

#

本地檔案的界定:指向乙個本地儲存的檔案,是乙個連線或者乙個對映

path1 = '

c:\\users\\11786\\desktop\\test.txt'#

正斜線兩個或者反斜線乙個來用於資料路徑的表達 再或者用r 寫在檔案路徑外面 推薦第三種

path2 = '

c:/users/11786/desktop/test.txt

'path3 = r'

c:\users\11786\desktop\test.txt

'print

(path1)

print

(path2)

print(path3)

#

讀取檔案 用open 語句

f = open(path1,'

r',encoding ='

utf8')

print

(type(f))

print

(f)print

(f.read())

#open('路徑','模式',encoding='編碼')

#模式 r:讀取檔案, 預設:w ;寫入:rw(讀取加寫入) a:追加

#簡答的讀取辦法 .read() 讀取後,游標會停留在讀取末尾

print('

二次讀取')

print

(f.read())

print('

二次讀取完畢')

#當執行第一次.read()之後,游標處於文件末尾 ,再次讀取輸出的結果為空

f.seek(0)

#移動游標的位置到文件開頭 0

print('

三次讀取')

print

(f.read())

print('

三次讀取完畢')

f.close()

print('

四次讀取')

print

(f.read())

print('

四次讀取完畢')

#print(f.read())關閉檔案後無法讀取

#關閉檔案鏈結 f.close()形成乙個好習慣

#

系統模組下的路徑操作方法 os模組提供了豐富的方法來處理檔案和目錄

import

osprint(os.name)#

輸出字串正在使用的平台,如果是windows則用nt 表示 對於linux則是posix

print(os.getcwd())#

返回當前路徑 當前python指令碼工作的目錄路徑

print(os.listdir())#

返回指定目錄下的所有檔案和目錄名

os.chdir(

'c:/users/11786/desktop/

')#切換到目標路徑

print

(os.getcwd())

print

(os.listdir())

#os.remove('test2.txt ') #刪除目標路徑下的檔案

flst = os.path.split('

c:/users/11786/desktop/test.txt

')#函式返回乙個路徑的目錄名和檔名

print

(flst,type(flst))

print(os.path.exists('

c:/users/11786/desktop/test.txt

'))#

判斷路徑下是否有檔案 或者路徑

print(os.path.exists('

c:/users/11786/desktop

'))

#建議先定義乙個工作的目錄 之後用相對路徑定位選擇就好

#

檔案的讀取與寫入

#檔案讀寫read

os.chdir('

c:/users/11786/desktop/')

f = open('

test.txt

','r

',encoding ='

utf8

')#txt儲存的時候的編碼設定 ansi-『gbk』 utf_8 -'utf8'

print

(f.read())

f.seek(0)

print(f.read(10))#

讀取檔案的前十個檔案 記得每次用seek將游標移動到文件開頭

f.seek(0)

f.seek(0)

print

(f.readline())

print(f.readline())#

按照行來讀取文件 一次性讀取一行

f.seek(0)

print(f.readline(10))#

讀取改行的前n個元素 f.readline(n)

#遍歷乙個檔案:for語句+f.readlines()

f.seek(0)

print

(f.readlines())

for line in

f.readlines():

print(line)

#

os.chdir('

c:/users/11786/desktop/')

f = open('

資料.txt

','r

',encoding ='

gbk

') #

#思路構建:觀察獲取的資料,獲取每一行 每一行中用:分隔的前面為名字 後面為具體經緯度資訊,於是先切片得到名字name

#隨後在information中得到具體的經緯度資訊 整理將其寫入空的字典中m 用n來計算迴圈的次數

m =n =0

f.seek(0)

for line in

f.readlines():

n+=1st1 = line.split(':'

) name =st1[0]

information = st1[1]

print

(information)

st2 = information.split(','

) lng =st2[0]

lat = st2[1]

add = st2[2].strip()

data = [['

name

', name],['

lng', lng],['

lat', lat],['

address

',add]]

print

(dict(data))

print(m[:10])

print('

\n總共轉換資料多少條

Python之資料處理

靠別人不如靠自己,學學學學學學學學!原資料 需求 coding utf 8 txtfile aminer1.txt newtxtfile open new txtfile,w with open txtfile,r as file to read lines file to read.readlin...

python資料處理之6 3

import numpy as np import pandas as pd from fuzzywuzzy import fuzz from fuzzywuzzy import process pd.set option display.width 500 設定輸出一行中顯示的數量 data pd...

Python 資料處理

將檔案切分,存入列表 strip split with open james.txt as jaf data jaf.readline james data.strip split 資料檔案為 2 34,3 21,2.34,2.45,3.01,2 01,2 01,3 10,2 22 print ja...