简体   繁体   English

读取 csv 文件的每 25 行并使用 python 传递给列表

[英]read every 25 line of a csv file and pass to list using python

I want to read a file and convert every 25 lines of that file into a list, that is, it should have 4 lists with 25 items in each (for 100 lines of a file).我想读取一个文件并将该文件的每 25 行转换为一个列表,也就是说,它应该有 4 个列表,每个列表中有 25 个项目(一个文件的 100 行)。 I am not being able to get the code for this problem.我无法获得此问题的代码。 The input file looks like this , in actual it has 100 lines:输入文件看起来像这样,实际上它有 100 行:

{'PutRequest': {'Item': {'id': {'S': 'E1DBEAE3'}, 'value': {'M': {'result': {'N': u'0.0015'}, 'lastupdatedtime': {'S': '2019-06-20'}}}}}}
{'PutRequest': {'Item': {'id': {'S': '31C6C'}, 'value': {'M': {'result': {'N': u'0.1129'}, 'lastupdatedtime': {'S': '2019-06-20'}}}}}}
{'PutRequest': {'Item': {'id': {'S': '59D40'}, 'value': {'M': {'result': {'N': u'0.00129'}, 'lastupdatedtime': {'S': '2019-06-20'}}}}}}
{'PutRequest': {'Item': {'id': {'S': 'A2A9'}, 'value': {'M': {'result': {'N': u'0.05129'}, 'lastupdatedtime': {'S': '2019-06-20'}}}}}}

Also, I want to prepend and append a string to the very first element and the last element of every list respectively, prepend string like :另外,我想分别在每个列表的第一个元素和最后一个元素前面加上一个字符串,在前面加上一个字符串,如:

'{"test":[' and append string like: ']}'

After prepend and append it should look like for a list size of 3 for example:在前置和附加之后,它应该看起来像列表大小为 3,例如:

{"test":[{'PutRequest': {'Item': {'id': {'S': 'E1DBEAE3'}, 'value': {'M': {'result': {'N': u'0.0015'}, 'lastupdatedtime': {'S': '2019-06-20'}}}}}}
{'PutRequest': {'Item': {'id': {'S': '31C6C'}, 'value': {'M': {'result': {'N': u'0.1129'}, 'lastupdatedtime': {'S': '2019-06-20'}}}}}}
{'PutRequest': {'Item': {'id': {'S': '59D40'}, 'value': {'M': {'result': {'N': u'0.00129'}, 'lastupdatedtime': {'S': '2019-06-20'}}}}}}
{'PutRequest': {'Item': {'id': {'S': 'A2A9'}, 'value': {'M': {'result': {'N': u'0.05129'}, 'lastupdatedtime': {'S': '2019-06-20'}}}}}}]}

I have tried this code:我试过这个代码:

from itertools import islice
list =[]
with open('output_of_json.json', 'r') as infile:
    lines_gen = islice(infile, 25)
    for line in lines_gen:
        list.append(line)

Unable to go past the first 25 lines of the file无法越过文件的前 25 行

This can be made into a function.这可以做成一个函数。 It's logically what you want.这在逻辑上就是你想要的。 You can use enumerate to clean it up.您可以使用 enumerate 来清理它。

with open(filename,'r') as f:

    counter = 25
    iteration = -1
    out_dict = {}            

    for i in f.readlines():

        if counter == 25:

            if out_dict[iteration]:

                # append your list
                out_dict[iteration].append('string here')

            counter = 0
            iteration += 1

            # create new instance and pre-pend
            out_dict[iteration] = ['string here']

        out_dict[iteration].append(i)

Look at the grouper function in itertools recipes .看看grouper在功能itertools食谱

import itertools

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return itertools.zip_longest(*args, fillvalue=fillvalue)

from itertools import islice
list =[]
with open('output_of_json.json', 'r') as infile:
    lines_gen = grouper(infile, 25, fillvalue='')
    for line in lines_gen:
        # whatever you want to do

Note that if the final block of lines has fewer than 25 lines, that code will fill out the 25 with blank lines.请注意,如果最后一行行少于 25 行,则该代码将用空行填充 25 行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM