python3 處理檔案

2021-07-24 18:03:25 字數 1175 閱讀 5037

fhand = open('text.txt')
python裡面的open()函式返回乙個file handler, 如果你print(fhand)的話, 得到一些跟檔案有關的資訊:

name='text.txt'

mode='r'

encoding='us-ascii'>

今天實現了乙個讀取每一行,分別輸出並統計行數的功能:

fhand = open('text.txt')

count = 0

forline

in fhand:

print(line)

count = count + 1

print('line count:', count)

乙個比較重要的問題就是路徑,如果你是用terminal在處理python的程式,那路徑就是與.py檔案同屬的目錄,如果你用idle, 那一般路徑可以在show path裡面查到,在選單欄的檔案中可以點進去找到,或者你可以用以下的方法:

>>>import sys

>>>sys.path

這是匯入了sys模組,使用其中的path命令,然後會跳出來可用目錄是哪些。

csv檔案是用逗號分隔開的資料,一般的**軟體如excel都能識別並使用;我在excel裡面隨便輸入了一些單詞,構成了乙個.csv檔案:

用文字行開啟就是:

用python處理csv檔案需要用到csv模組,今天還是實現了乙個簡單的輸出所有行的功能:

import csv

reader = csv.reader(f)

for each_row in reader:

print(each_row)

Python3處理HTTP請求

python3處理http請求的包 http.client,urllib,urllib3,requests 其中,http 比較 low level,一般不直接使用 urllib更 high level一點,屬於標準庫。urllib3跟urllib類似,擁有一些重要特性而且易於使用,但是屬於擴充套件...

Python3處理日期與時間

import time 獲取當前時間的時間戳 print time.time 獲取10位時間戳 print int time.time 獲取13位時間戳 示例 import time 時間戳 結構化時間元組 print time.localtime print time.localtime time...

Python3處理檔案中每個詞的方法

created on dec 21,2012 處理檔案中的每個詞 author liury lab import codecs the file codecs.open d text.txt ru utf 8 for line in the file for word in line.split p...