Mail lines in a list and iteration file

I want to create a dictionary in which the keys are taken from a list keys, and the values ​​are lists of strings taken from several text files. Suppose the list keysand all files to read have the same number of lines.

How can I iterate over the list keysand lines of each file at the same time? My idea is to use zip(), but it did not work for me.

I know that I can iterate over lines in a file using:

currFile = open('myfile.txt', 'r')
for line in currFile:
    # Do something

And I know that I can iterate over two lists at the same time:

for foo, bar in zip(foos, bars):
    # Do something

But this does not work:

myDict = {}
keys = [17, 21, 8, 2, ..., 91]
currFile = open('myfile.txt', 'r')
for key, line in zip(keys, currFile):
    myDict[key] = line

I could pull all the lines from the file to the list, pin it, and then run the loop, but that would not be very efficient.

How can I iterate over the list keysand lines in a file at the same time so that zip () is called dynamically?

+4
5

, , - , . , .

, . file1:

line0
line1
line2
line3

file2:

line5
line6
line7
line8

files (, [open('file1','r'), open('file2','r')]).

from collections import defaultdict as ddict
d = ddict(list)
for number,lines in enumerate(zip(*files)):
    for line in lines:
        d[number].append(line)

Python3, Python2.x, izip. :

for file in files:
    file.close()

d:

defaultdict(<type 'list'>, {0: ['line0\n', 'line5\n'], 1: ['line1\n', 'line6\n'], 2: ['line2\n', 'line7\n'], 3: ['line3\n', 'line8\n']})
+1
def add_to_dict(someDict, filename, someNums):
    with open(filename, 'r') as f:
        for num, line in enumerate(f):
            if num in someNums:
                if num not in someDict:
                    someDict[num] = []
                someDict[num].append(line)

myDict = {}

lineNums = [2,45,13,56]

add_to_dict(myDict, "file1.txt", lineNums)
add_to_dict(myDict, "file2.txt", lineNums)

EDIT: defaultdict(list) Sahand,

if num not in someDict:
    someDict[num] = []
0

- , collections.defaultdict enumerate, :

EDIT: It's probably best to just pass the file names of the function itself:

from collections import defaultdict


def lines_to_dictionary(*files):
    result = defaultdict(list)
    for file_name in files:
        with open(file_name, 'r') as f:
            for line_number, line in enumerate(f):
                result[line_number].append(line.strip())
    return result


result = lines_to_dictionary('1.csv', '2.csv')

print result[0]  # Prints a list of first lines in all files
print result[1]  # Prints a list of second lines in all files, etc...
0
source

Using standard dict and zip, one file at a time. May replace open / close with "c". As already mentioned, one file at a time might be better for error handling.

d = {}
keys = set([k1, k2, ...])
files = [f1, f2, ...]
for f in files:
    ifs = open(f)
    for n, line in enumerate(ifs):
        if n in keys:
            d.setdefault(n, []).append(line)
    ifs.close()
0
source
keys = [1, 2, 3, 4]
files = [open('a'), open('b'), open('c'), open('d')]

for x in zip(keys, *files):
    print x

Edit: your original idea is correct, there is simply no syntax for expanding the list of files.

0
source

Source: https://habr.com/ru/post/1546619/


All Articles