SQLite transaction for CSV importing

前端 未结 3 1596
眼角桃花
眼角桃花 2020-12-19 21:56

I\'m very new to python and have been working on my raspberry pi to get a script up and running to import millions of sensor data records into sqlite. I want to do this in t

3条回答
  •  一向
    一向 (楼主)
    2020-12-19 22:39

    The problem is that the object you get from the csv.reader method does not support the len() function. In fact, this CSV reader reads only when instructed to do so, and as such, it does not know how many records are in the file.

    As such, you need to update the chunks method to deal with not knowing the amount of things to be chunked. Replace that function with this:

    def chunks(data, n=10000):
        buffer = [None] * n
        idx = 0
        for record in data:
            buffer[idx] = record
            idx += 1
            if idx == n:
                yield buffer
                buffer = [None] * n
                idx = 0
        if idx > 0:
            yield buffer[:idx]
    

    What this does is: it keeps retrieving records from your file as long as there are records to retrieve, and every n rows, emits a list of n records. For example:

    >>> for c in chunks(range(10), 3):
    ...     print(c)
    ...
    [0, 1, 2]
    [3, 4, 5]
    [6, 7, 8]
    [9]
    

提交回复
热议问题