導航:首頁 > 編程語言 > python文件讀寫

python文件讀寫

發布時間:2022-01-24 08:35:40

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'])

(1)python文件讀寫擴展閱讀:

python控制語句

1、if語句,當條件成立時運行語句塊。經常與else, elif(相當於else if) 配合使用。

2、for語句,遍歷列表、字元串、字典、集合等迭代器,依次處理迭代器中的每個元素。

3、while語句,當條件為真時,循環運行語句塊。

4、try語句,與except,finally配合使用處理在程序運行中出現的異常情況。

5、class語句,用於定義類型。

6、def語句,用於定義函數和類型的方法。

⑵ 如何讀取python的.py文件

你說的環境變數其實是系統的環境變數,

進入python後,還要配置python自己的環境變數。

首先,你應該進入C:Usersqc後,再啟動python

cdC:Usersqc
python
>>>importex25

如果從其它地方進入python:

CMD進入python後,要加入路徑:

>>>importsys
>>>sys.path.insert(0,"C:\Users\qc")
>>>importex25

⑶ python 讀取文件

#!/usr/bin/python2.7
import random,re
f0=file('proxys.txt','r')
dat0=f0.readlines()
f0.close()
#提取含有$1sec的行(我理解你想按sec的大小排序。)
dat1=[]
for i in dat0:
dat1.append((i,re.search(r'\$(\d+)sec',i).group(1)))
#現在dat1裡面的數據是在原來的每一行前面加了一列sec的值。
dat2=[]
for i in dat1:
if i[0]==1:
dat2.append(i[1])
#現在取出了所有sec==1的行,隨機取一行
dat3=random.choice(dat2)
c1=re.search(r'((\d{1,3}\.?){4}):(\d+)',dat3).group(1)
c2=re.search(r'((\d{1,3}\.?){4}):(\d+)',dat3).group(3)

⑷ python 文件讀寫

你的filename是多少,看提示貌似是你的文件不存在活著路徑錯誤

⑸ Python如何讀寫文本文件

1.open使用open打開文件後一定要記得調用文件對象的close()方法。比如可以用try/finally語句來確保最後能關閉文件。
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
註:不能把open語句放在try塊里,因為當打開文件出現異常時,文件對象file_object無法執行close()方法。
2.讀文件讀文本文件input = open('data', 'r')
#第二個參數默認為r
input = open('data')

讀二進制文件input = open('data', 'rb')
讀取所有內容file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )
讀固定位元組file_object = open('abinfile', 'rb')
try:
while True:
chunk = file_object.read(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_object.close( )
讀每行list_of_all_the_lines = file_object.readlines( )
如果文件是文本文件,還可以直接遍歷文件對象獲取每行:
for line in file_object:
process line
3.寫文件寫文本文件output = open('data.txt', 'w')
寫二進制文件output = open('data.txt', 'wb')
追加寫文件output = open('data.txt', 'a')

output .write("\n都有是好人")

output .close( )

寫數據file_object = open('thefile.txt', 'w')
file_object.write(all_the_text)
file_object.close( )

閱讀全文

與python文件讀寫相關的資料

熱點內容
安卓如何查看異常重啟 瀏覽:715
解壓音樂排名 瀏覽:383
安卓手機瀏覽器怎麼掃二維碼 瀏覽:715
通達信成本均線源碼 瀏覽:614
可以下載的解壓音頻 瀏覽:564
海賊王怎麼換伺服器 瀏覽:318
計算機上的共享文件夾映射 瀏覽:940
榮耀安裝包在文件夾哪裡 瀏覽:195
機票php源碼 瀏覽:231
linux共享mac 瀏覽:923
中國沒有國外的伺服器地址 瀏覽:759
為什麼退款伺服器連接錯誤 瀏覽:557
android簡訊存儲位置 瀏覽:972
unix網路編程卷4 瀏覽:808
找靚機app下單什麼時候發貨 瀏覽:413
android一個應用兩個進程 瀏覽:803
linux硬碟復制 瀏覽:808
php圖片伺服器搭建 瀏覽:801
下載壓縮文件怎麼打開 瀏覽:194
新建文件夾叫什麼名字 瀏覽:567