Link to home
Start Free TrialLog in
Avatar of jay_eire
jay_eireFlag for United States of America

asked on

Open 2 seperate filez create a combined list

Hello, I have 2 different text files with numerical data can i use one function to open the two files and create a combined list in python?  

StudentFees1_Oct
45
59
99
109

StudentFees2_Sept
79
33
55
111

the list would be something like this...
[45, 59, 99, 109, 79, 33, 55, 111]
ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jay_eire

ASKER

Hi Pepr thank you for the reply. This isn't a homework question I'm afraid. I am experimenting with python only a newbie and I'm trying to move away from coding using dreamweaver. This was a basic reconciliation accounts script I was working on.

Thank you for the tips and the link to the python iterable.
Thanks for the swift response and tips.
Avatar of pepr
pepr

Is the StudentFees1_Oct name of the file or the content of the first line in the file? If it is not the part of the file content, try the following script:
#!python3
import itertools

with open('file1.txt') as f1, open('file2.txt') as f2:
    lst = [int(line) for line in itertools.chain(f1, f2)]

print(lst)

Open in new window

The with construct ensures that the file object f1 and f2 are closed after the body of the construct is executed. It can be replaced by the plain f1 = open() and f.close() pairs. However, my advice is to get used to the with construct.

Feel free to ask here for what should be explained better. The question should get the answer if it really is a question ;)
Hi pepr thank you for taking the time to put that script together!

StudentFees1_Oct & StudentFees2_Sept are the file names they are a couple of MB's in size I just posted a brief sample of the data within.  I have automated the script now using windows task manager its working a treat.

Thanks once again.
If the files are that big, would not it be possible to process them without filling the list first? The problem is that the list is stored in memory and the application can be unneccessarily memory-greedy.

It could be the case that there is more than just two files, or the number of files is not fixed. Then the program can be modified so that you prepare the list of the filenames, and then process the files one by one. The itertools.chain actually does the same. The problem is that the files must be opened in advance.

Try the following:
#!python3

fnames = ['StudentFees1_Oct.txt', 'StudentFees2_Sept.txt']

for fname in fnames:
    with open(fname) as f:
        for line in f:
            value = int(line)    # extract the value from the line
            print(value)         # process the value

Open in new window

Thanks again pepr.