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]
LVL 7
jay_eireAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peprCommented:
I guess it is a homework. because of that, I will give you only the ideas.

The itertools.chain() iterates through the first iterable, then through the next, etc. See the doc http://docs.python.org/3.3/library/itertools.html#itertools.chain.

The open text file behaves as such iterable. This way you can concatenate the content of the two open files.

The iterated element of the text file is one line including the \n as a string. You require to convert that string into the integer value.

The list() takes whatever iterable and returns the list of items. Or you can use the list comprehension construct alternatively, or the for-loop.

After opening the input files, the code can be a one-liner.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jay_eireAuthor Commented:
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.
0
jay_eireAuthor Commented:
Thanks for the swift response and tips.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

peprCommented:
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 ;)
0
jay_eireAuthor Commented:
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.
0
peprCommented:
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

0
jay_eireAuthor Commented:
Thanks again pepr.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.