2017-03-09 44 views
1

所以,我有一個數據文件有3列。我想要做的是創建一個函數,它將起始和結束行號作爲輸入。喜歡的東西:按行位置讀取文件

def(start line number, end line number): 
    with open("data.txt", 'r') as f: 
     for line in f: 
      splitted_line = line.strip().split(",") 
      date1 = datetime.strptime(splitted_line[0],'%Y%m%d:%H:%M:%S.%f') 
      price = float(splitted_line[1]) 
      volume = int(splitted_line[2]) 
      my_tuple=(date1,price,volume) 
+0

@J Doe你應該接受一個答案,如果一個人爲你工作。 –

回答

1
def func(start,end): 
    with open("data.txt", 'r') as f: 
     for idx,line in enumerate(f): 
      if idx == end: 
      break 
      if idx < start: 
      continue 

      splitted_line = line.strip().split(",") 
      date1 = datetime.strptime(splitted_line[0],'%Y%m%d:%H:%M:%S.%f') 
      price = float(splitted_line[1]) 
      volume = int(splitted_line[2]) 
      my_tuple=(date1,price,volume) 
0

如果我正確地讀這篇文章,這個功能應該只讀這是在範圍[start_line, end_line](我假設這是一個包容性的範圍,即要編號的行同時閱讀開始和結束行)。爲什麼不是write your for loop with enumeration,並簡單地跳過超出傳遞範圍的行?

def read_line_range_inclusive(start_line, end_line): 
    filename = "data.txt" 
    with open(filename) as f: 
     for i, line in enumerate(f): 
      if i < start_line: # will read the start line itself 
       continue # keep going... 
      if i > end_line: # will read the end line itself 
       break # we're done 

      # ... perform operations on lines ... 

另外,用逗號分隔時要小心;這適用於簡單的行,如1,2,3,但1,2,"a,b,c",3,​​不應該拆分成單獨的列呢?我建議使用built-in csv module,它會自動處理這些邊緣情況:

import csv 

def read_line_range_inclusive(start_line, end_line): 
    filename = "data.txt" 
    with open(filename) as f: 
     for i, row in enumerate(csv.reader(f)): 
      # row will already be separated into list 
      # ... proceed as before ... 

請注意,您只能使用with聲明文件對象本身,not on the csv.reader parsed file上,所以這是行不通的:with csv.reader(open(filename)) as f:

0

如果使用CSV閱讀器,可以訪問行號:

csvreader.line_num 

的行數從源迭代器讀取。這不是與返回的記錄數相同的 ,因爲記錄可以跨越多個 行。

0

我們可以linecache模塊相結合,csv來完成這項工作:

import csv 
import linecache 


def get_lines(filename, start_line_number, end_line_number): 
    """ 
    Given a file name, start line and end line numbers, 
    return those lines in the file 
    """ 
    for line_number in range(start_line_number, end_line_number + 1): 
     yield linecache.getline(filename, line_number) 


if __name__ == '__main__': 
    # Get lines 4-6 inclusive from the file 
    lines = get_lines('data.txt', 4, 6) 
    reader = csv.reader(lines) 

    for row in reader: 
     print(row) 

考慮數據文件,data.txt中:

# this is line 1 
# line 2 

501,john 
502,karen 
503,alice 

# skip this line 
# and this, too 

上面的代碼將產生如下輸出:

['501', 'john'] 
['502', 'karen'] 
['503', 'alice'] 

討論

  • linecache是一個鮮爲人知的庫,它允許用戶快速地從文本文件
  • csv檢索行是爲了對付逗號分隔值
  • 將它們組合庫,我們可以輕輕鬆鬆完成工作