Sheila Evans
Sheila Evans

Reputation: 9

Combining two files' data into one list

I am new to python and have only started working with files. I am wondering how to combine the data of two files into one list using list comprehension to read and combine them.

#for instance line 1 of galaxies = I
#line 1 of cycles = 0
#output = [IO] (list)

This is what I have so far. Thanks in advance!

 comlist =[line in open('galaxies.txt') and line in open('cycles.txt')]

Update:

 comlist = [mylist.append(gline[i]+cline[i]) for i in range(r)]

However, this is only returning none

Upvotes: 0

Views: 1419

Answers (6)

martineau
martineau

Reputation: 123463

Like this:

#from itertools import chain

def chainer(*iterables):
    # chain('ABC', 'DEF') --> A B C D E F
    for it in iterables:
        for element in it:
            yield element

comlist = list(chainer(open('galaxies.txt'), open('cycles.txt')))
print(comlist)

Although leaving files open like that isn't generally considered a good practice.

Upvotes: 2

Trevor Merrifield
Trevor Merrifield

Reputation: 4691

Using only list comprehensions:

[line for file in (open('galaxies.txt'), open('cycles.txt')) for line in file]

However it is bad practice to leave files open and hope the GC cleans it up. You should really do something like this:

import fileinput
with fileinput.input(files=('galaxies.txt', 'cycles.txt')) as f:
    comlist = f.readlines()

If you want to strip end of line characters a good way is with line.rstrip('\r\n').

Upvotes: 0

Chiheb Nexus
Chiheb Nexus

Reputation: 9257

You can achieve your task within lambda and map:

I assume a data in in_file (first file) like this :

1 2
3 4
5 6
7 8

And a data in in_file2 (second file) like this:

hello there!

And with this piece of code:

# file 1
a = "in_file"
# file 2
b = "in_file2"
f = lambda x,y: (open(x, 'r'),open(y, 'r'))
# replacing "\n" with an empty string
data = [k for k in map(lambda x:x.read().replace("\n",""), f(a,b))]
print(data)

The output will be:

['1 23 45 67 8', 'hello there!']

However, it's not a good practice to leave an opened files like this way.

Upvotes: 0

Paul Cornelius
Paul Cornelius

Reputation: 10936

f1 = open('galaxies.txt')
f2 = open('cycles.txt')

If you want to combine them by alternating the lines, use zip and comprehension:

comlist = [line for two_lines in zip(f1, f2) for line in two_lines]

You need two iterations here because the return value from zip is itself an iterable, in this case consisting of two lines, one from f1 and one from f2. You can combine two iterations in a single comprehension as shown.

If you want to combine them one after the other, use "+" for concatenation:

comlist = [line for line in f1] + [line for line in f2]

In both cases, it's a good practice to close each file:

f1.close()
f2.close()

Upvotes: 0

ssm
ssm

Reputation: 5373

If its only 2 files why do you want to use comprehension together? Something like this would be easier:

[l for l in open('galaxies.txt')]+[l for l in open('cycles.txt')]

The question is, what if you had n files? lets say in a list ... fileList = ['f1.txt', 'f2.txt', ... , 'fn.txt']. Then you may consider itertools.chain

import itertools as it
filePointers = map(open, fileList)
lines = it.chain(filePointers)
map(close, filePointers)

I haven't tested it, but this should work ...

Upvotes: 0

Josh J
Josh J

Reputation: 6893

You can use zip to combine iterables

https://docs.python.org/3/library/functions.html#zip

Upvotes: 1

Related Questions