pythonfileread
⑴ python如何從文件讀取數據
1.1 讀取整個文件
要讀取文件,需要一個包含幾行文本的文件(文件PI_DESC.txt與file_reader.py在同一目錄下)
PI_DESC.txt
3.1415926535
8979323846
2643383279
5028841971
file_reader.py
with open("PI_DESC.txt") as file_object:
contents = file_object.read()
print(contents)
我們可以看出,讀取文件時,並沒有使用colse()方法,那麼未妥善的關閉文件,會不會導致文件收到損壞呢?在這里是不會的,因為我們在open()方法前邊引入了關鍵字with,該關鍵字的作用是:在不需要訪問文件後將其關閉
1.2文件路徑
程序在讀取文本文件的時候,如果不給定路徑,那麼它會先在當前目錄下進行檢索,有時候我們需要讀取其他文件夾中的路徑,例如:
⑵ Python讀取文件內容的方法有幾種
filename=open('i:\\install\\test.txt','r+')#讀取xx路徑xx文件;r+代表的是讀寫並存方式 print filename.read()#讀取所有的文件
⑶ 如何用python通過read()方法統計text1.txt文件中數字、空格、字母出現的次數
(1)先讀取文件(假設文件的目錄在C盤):
file=open("C:\text.txt","r")
res=file.read()#讀取內容
file.close()#關閉
(2)統計:
#出現的次數要用count()方法
#空格出現的次數
a1=res.count("")
print(a1)#輸出
#數字出現的次數
i=0
forjinrange(11):#for循環
i+=res.count(str(j))
print(i)#輸出
#字母出現的次數更麻煩,因為太多了,包括大寫和小寫。
i=0#初始化變數i
#先統計大寫字母
forjinrange(65,91):
i+=res.count(str(chr(j)))
#在統計小寫字母
forjinrange(97,123):
i+=res.count(str(chr(j)))
print(i)#輸出
(3)完整代碼:
file=open("C:\text.txt","r")
res=file.read()#讀取內容
file.close()#關閉
#出現的次數要用count()方法
#空格出現的次數
a1=res.count("")
print(a1)#輸出
#數字出現的次數
i=0
forjinrange(11):#for循環
i+=res.count(str(j))
print(i)#輸出
#字母出現的次數更麻煩,因為太多了,包括大寫和小寫。
i=0#初始化變數i
#先統計大寫字母
forjinrange(65,91):
i+=res.count(str(chr(j)))
#在統計小寫字母
forjinrange(97,123):
i+=res.count(str(chr(j)))
print(i)#輸出
效果
(5)看不懂的代碼代碼請追問,如有幫助請採納
⑷ python對文件的讀操作方法有哪些
摘要 1 文件讀取全文本操作
⑸ python讀取文件read file, 從中計算平均值和最大值, 最小值 。
因為你將min_num初始化為0了
只有當其他的數字小於min_num,你才將那個值賦予min_num
可是,其他的數字都比min_num大,所有不會去改變min_num的值,它就是0
你應該,一開始,讀第一個數字,就把max、min都附上第一個數字
後面慢慢去比較和更新
按照你的邏輯,如果所有的數字是負數,你的最大值出來的肯定是0
⑹ 怎麼使python文件.py擁有『讀取』屬性(read。()),使得可以利用cmd讀取python文件
with open('file','r')as f:
readfile=f.read()
print(readfile)
⑺ 新人python read file和 2D List 問題求助,急!!
#!/usr/bin/env python
#-*- encoding: utf-8 -*-
with open('MetaDataStationList.csv') as f:
f.readline()
for n, line in enumerate(f):
name = line.split(',')[1]
print(n, name)
python3上測試的,python2上應該會需要修改一下print的格式
⑻ python如何讀取文件的內容
# _*_ coding: utf-8 _*_
import pandas as pd
# 獲取文件的內容
def get_contends(path):
with open(path) as file_object:
contends = file_object.read()
return contends
# 將一行內容變成數組
def get_contends_arr(contends):
contends_arr_new = []
contends_arr = str(contends).split(']')
for i in range(len(contends_arr)):
if (contends_arr[i].__contains__('[')):
index = contends_arr[i].rfind('[')
temp_str = contends_arr[i][index + 1:]
if temp_str.__contains__('"'):
contends_arr_new.append(temp_str.replace('"', ''))
# print(index)
# print(contends_arr[i])
return contends_arr_new
if __name__ == '__main__':
path = 'event.txt'
contends = get_contends(path)
contends_arr = get_contends_arr(contends)
contents = []
for content in contends_arr:
contents.append(content.split(','))
df = pd.DataFrame(contents, columns=['shelf_code', 'robotid', 'event', 'time'])
(8)pythonfileread擴展閱讀:
python控制語句
1、if語句,當條件成立時運行語句塊。經常與else, elif(相當於else if) 配合使用。
2、for語句,遍歷列表、字元串、字典、集合等迭代器,依次處理迭代器中的每個元素。
3、while語句,當條件為真時,循環運行語句塊。
4、try語句,與except,finally配合使用處理在程序運行中出現的異常情況。
5、class語句,用於定義類型。
6、def語句,用於定義函數和類型的方法。
⑼ python read file數字和文字
你好:
你的數據,如果是每行一條的話;
按行讀取,
每行按「,」分割,然後重新組合,
就行,
不明白的地方請追問!