我如何知道文件指针在哪里,以便我可以识别fi中的起始位置

2024-04-29 10:32:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理超过6毫米行的股票代码数据。我想抓取一个符号的所有数据,做我需要的处理,并输出结果。在

我写过代码,告诉我每一个股票代码从哪一行开始(见下面的代码)。我在想,如果我知道一个新符号从什么位置开始(而不是行号),这样我就可以使用seek(#)轻松地跳转到股票代码的起始位置。我也很好奇如何扩展这个逻辑来读取股票行情器的整个数据块(起始位置到结束位置)。在

import csv
data_line       = 0 # holds the file line number for the symbol
ticker_start        = 0
ticker_end          = 0
cur_sec_ticker  = ""
ticker_dl   = [] # array for holding the line number in the source file for the start of each ticker
reader = csv.reader(open('C:\\temp\sample_data.csv', 'rb'), delimiter=',')
for row in reader:
    if cur_sec_ticker != row[1]:   # only process a new ticker
        ticker_fr = str(data_line) + ',' + row[1] # prep line for inserting into array

        # desired line for inserting into array, ticker_end would be the last 
        # of the current ticker data block, which is the start of the next ticker
        # block (ticker_start - 1)
        #ticker_fr = str(ticker_start) + str(ticker_end) + str(data_line) + ',' + row[1] 

        print ticker_fr
        ticker_dl.append(ticker_fr)
        cur_sec_ticker  = row[1]
    data_line += 1
print ticker_dl

下面我放了一个小样本来说明数据文件:

^{pr2}$

Tags: csvthe数据fordatalinesecfr
1条回答
网友
1楼 · 发布于 2024-04-29 10:32:11

通常,您可以使用tell方法获取文件对象的当前位置。但是,如果当前代码将文件读取委托给csv模块,则可能很难使用该代码。逐行读取时甚至很难做到这一点,因为底层的file对象可能会被读入比一行更大的块中(方法readline和{}方法在后台做了一些缓存以隐藏这一点)。在

如果你需要的话,你可以把整个文件都放在你自己读的时候。^{cd1>可能不是必要的。在

类似这样的方法可以读取数据块,然后将其拆分为行和值,同时跟踪到目前为止读取了多少字节:

def generate_values(f):
    buf = "" # a buffer of data read from the file
    pos = 0  # the position of our buffer within the file

    while True: # loop until we return at the end of the file
        new_data = f.read(4096) # read up to 4k bytes at a time

        if not new_data: # quit if we got nothing
            if buf:
                yield pos, buf.split(",") # handle any data after last newline
            return

        buf += new_data
        line_start = 0 # index into buf

        try:
            while True: # loop until an exception is raised at end of buf
                line_end = buf.index("\n", line_start) # find end of line
                line = buf[line_start:line_end] # excludes the newline

                if line: # skips blank lines
                    yield pos+line_start, line.split(",") # yield pos,data tuple

                line_start = line_end+1
        except ValueError: # raised by `index()`
            pass

        pos += line_end + 1
        buf = buf[line_end + 1:] # keep left over data from end of the buffer

如果文件的结尾不是\n,那么这可能需要稍作调整,但应该不会太难。在

相关问题 更多 >