• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

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?  



the list would be something like this...
[45, 59, 99, 109, 79, 33, 55, 111]
  • 4
  • 3
1 Solution
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.
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.
jay_eireAuthor Commented:
Thanks for the swift response and tips.
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

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:
import itertools

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


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 ;)
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.
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:

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

jay_eireAuthor Commented:
Thanks again pepr.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now