1. python和MATLAB讀取excel指定行列數據的方法
1、用xlrd讀取
對應方法如下,需要先import xlrd和numpy,通過row_start和row_end控制行數,通過column_start和column_end控制列數
這里要注意python是0-based索引,excel看的時候是1-based的索引
2、用pandas下的read_excel函數
dframe = pd.read_excel(「file_name.xlsx」)
dframe = pd.read_excel(「file_name.xlsx」, sheetname=」Sheet_name」)
dframe = pd.read_excel(「file_name.xlsx」, sheetname=number)
讀取表格的方式有兩種:
1、xlsread
[~,MeaDef,~]=xlsread(xls_site,table_tag,'B12:AI12');
這里輸出是一個對應數字,一個對應字元串,一個是用元胞把所有數據放在一起
對應的xlswrite格式:
xlswrite(xls_site_output,train,1,['A',num2str(ix+1),':M',num2str(ix+1)])
2、read_table
2. python讀取excel文件,將每一行都保存為一個列表。每一行對應一個列表。
python讀寫excel文件要用到兩個庫:xlrd和xlwt,首先下載安裝這兩個庫。
1、#讀取Excel
importxlrd
data = xlrd.open_workbook(excelFile)
table = data.sheets()[0]
nrows = table.nrows #行數
ncols = table.ncols #列數
for i in xrange(0,nrows):
rowValues= table.row_values(i) #某一行數據
for item in rowValues:
printitem
2、寫Excel文件
'''往EXCEl單元格寫內容,每次寫一行sheet:頁簽名稱;row:行內容列表;rowIndex:行索引;
isBold:true:粗欄位,false:普通字體'''
defWriteSheetRow(sheet,rowValueList,rowIndex,isBold):
i = 0
style = xlwt.easyxf('font: bold 1')
#style = xlwt.easyxf('font: bold 0, color red;')#紅色字體
#style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;') # 設置Excel單元格的背景色為黃色,字體為粗體
forsvalue inrowValueList:
strValue = unicode(str(svalue),'utf-8')
ifisBold:
sheet.write(rowIndex,i,strValue,style)
else:
sheet.write(rowIndex,i,strValue)
i = i + 1
'''寫excel文件'''
defsave_Excel(strFile):
excelFile = unicode(strFile,"utf8")
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet1',cell_overwrite_ok=True)
headList = ['標題1','標題2','標題3','標題4','總計']
rowIndex = 0
WriteSheetRow(sheet,headList,rowIndex,True)
fori inxrange(1,11):
rowIndex = rowIndex + 1
valueList = []
forj inxrange(1,5):
valueList.append(j*i)
WriteSheetRow(sheet,valueList,rowIndex,False)
wbk.save(excelFile)
style2 = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;')
在設置上Excel單元格的背景色時,fore_colour支持的顏色是有限的,僅支持一下顏色
aqua 0x31
black 0x08
blue 0x0C
blue_gray 0x36
bright_green 0x0B
brown 0x3C
coral 0x1D
cyan_ega 0x0F
dark_blue 0x12
dark_blue_ega 0x12
dark_green 0x3A
dark_green_ega 0x11
dark_purple 0x1C
dark_red 0x10
dark_red_ega 0x10
dark_teal 0x38
dark_yellow 0x13
gold 0x33
gray_ega 0x17
gray25 0x16
gray40 0x37
gray50 0x17
gray80 0x3F
green 0x11
ice_blue 0x1F
indigo 0x3E
ivory 0x1A
lavender 0x2E
light_blue 0x30
light_green 0x2A
light_orange 0x34
light_turquoise 0x29
light_yellow 0x2B
lime 0x32
magenta_ega 0x0E
ocean_blue 0x1E
olive_ega 0x13
olive_green 0x3B
orange 0x35
pale_blue 0x2C
periwinkle 0x18
pink 0x0E
plum 0x3D
purple_ega 0x14
red 0x0A
rose 0x2D
sea_green 0x39
silver_ega 0x16
sky_blue 0x28
tan 0x2F
teal 0x15
teal_ega 0x15
turquoise 0x0F
violet 0x14
white 0x09
yellow 0x0D"""
另外一種方式是 用pyExcelerator
from pyExcelerator import *# excel 第一行數據excel_headDatas = [u'發布時間', u'文章標題', u'文章鏈接', u'文章簡介']
articles =[
{u'發布時間':u'2017年5月9日',
u'文章標題':u'Python項目實戰教程:國內就能訪問的google搜索引擎',
u'
u'文章簡介':u'大家可以留言、想了解python那個方向的知識、不然我也不知道'},
{u'發布時間':u'2017年5月4日',
u'文章標題':u'對於學習Django的建議、你知道的有那些',
u'文章鏈接':',
u'文章簡介':u'隨著Django1.4第二個候選版的發布,雖然還不支持Python3,但Django團隊已經在著手計劃中,據官方博客所說,Django1.5將會試驗性的支持python3'}
]# 定義excel操作句柄excle_Workbook = Workbook()
excel_sheet_name = time.strftime('%Y-%m-%d')
excel_sheet = excle_Workbook.add_sheet(excel_sheet_name)
index = 0#標題for data in excel_headDatas:
excel_sheet.write(0, index, data)
index += 1index = 1#內容for article in articles:
colIndex = 0 for item in excel_headDatas:
excel_sheet.write(index, colIndex, article[item])
colIndex += 1
index += 1#保存test.xlsx到當前程序目錄excle_Workbook.save('test.xlsx')# db = mongoDB.mongoDbBase()# db.Get_information_stat()
3. python怎樣實現一行一行讀取文件數據,並且要實現讀取一行數據,就進行條件判斷
超簡單
get='''12
15
16
19
23'''
result=get.split(' ')
foriinresult:
ifint(i)>16:
print(i)
滿意還請採納
4. 怎樣用python,讀取excel中的一列數據
用python讀取excel中的一列數據步驟如下:
1、首先打開dos命令窗,安裝必須的兩個庫,命令是:pip3 installxlrd;Pip3 install xlwt。
5. Python如何讀取csv文件某一列的每一行數據,並判斷該數值是否滿足條件
讀取csv文件,用的是csv.reader()這個方法。返回結果是一個_csv.reader的對象,我們可以對這個對象進行遍歷,輸出每一行,某一行,或某一列。代碼如下:
6. 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文件路徑
程序在讀取文本文件的時候,如果不給定路徑,那麼它會先在當前目錄下進行檢索,有時候我們需要讀取其他文件夾中的路徑,例如:
7. python如何實現分行提取指定字元串
python讀取文件內容的方法:一.最方便的方法是一次性讀取文件中的所有內容並放置到一個大字元串中:all_the_text=open('thefile.txt').read()#文本文件中的所有文本all_the_data=open('abinfile','rb').read()#二進制文件中的所有數據為了安全起見,最好還是給打開的文件對象指定一個名字,這樣在完成操作之後可以迅速關閉文件,防止一些無用的文件對象佔用內存。舉個例子,對文本文件讀取:file_object=open('thefile.txt')try:all_the_text=file_object.read()finally:file_object.close()不一定要在這里用Try/finally語句,但是用了效果更好,因為它可以保證文件對象被關閉,即使在讀取中發生了嚴重錯誤。二.最簡單、最快,也最具Python風格的方法是逐行讀取文本文件內容,並將讀取的數據放置到一個字元串列表中:list_of_all_the_lines=file_object.readlines()這樣讀出的每行文本末尾都帶有"\n"符號;如果你不想這樣,還有另一個替代的辦法,比如:list_of_all_the_lines=file_object.read().splitlines()list_of_all_the_lines=file_object.read().split('\n')list_of_all_the_lines=[L.rstrip('\n')forLinfile_object]
8. Python按行讀取文件的簡單實現方法
Python按行讀取文件的簡單實現方法
下面小編就為大家帶來一篇Python按行讀取文件的簡單實現方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。
1:readline()
file = open("sample.txt")
while 1:
line = file.readline()
if not line:
break
pass # do something
file.close()
一行一行得從文件讀數據,顯然比較慢;
不過很省內存;
測試讀10M的sample.txt文件,每秒大約讀32000行;
2:fileinput
import fileinput
for line in fileinput.input("sample.txt"):
pass
寫法簡單一些,不過測試以後發現每秒只能讀13000行數據,效率比上一種方法慢了兩倍多;
3:readlines()
file = open("sample.txt")
while 1:
lines = file.readlines(100000)
if not lines:
break
for line in lines:
pass # do something
file.close()
用同樣的數據測試,它每秒可以讀96900行數據!效率是第一種方法的3倍,第二種方法的7倍!
4:文件迭代器
每次只讀取和顯示一行,讀取大文件時應該這樣:
file = open("sample.txt")
for line in file:
pass # do something
file.close()
以上就是小編為大家帶來的Python按行讀取文件的簡單實現方法全部內容了
9. 如何用python讀取文本中指定行的內容
txt文件可以用行號,用readlines讀取出來的數據是一個列表,你可以使用:
f = open('', 'r')
line = f.readlines()
line_need = line[行號-1]
這樣來取指定行
10. python提供了一組讀取文件數據的方法,使用什麼方法可以讀取當前行內
可以使用 Python 的內置函數 open() 和 readline() 來讀取文件的當前行內容。
舉個例子:
這里的 readlines() 函數會將文件中所有行讀取到一個列表中,而迭代文件對象直接返迴文件中的下一行。