Reading files into a 2D list.

O

Oyvind Ostlund

I am not sure what the right syntax is here. So please help me out (started 2 days ago).

I have a list of about 20 files that I want to read line by line into a 2D list. So the first dimension will be each file, and the second every line in that file.


I tried to do something like this:

files_and_lines = [][]
filenumber = 0

for files in file_names:
try:
lexi_file = open(str(sys.path[0]) + "/lexi/" + files, "r")
files_and_lines[filenumber] = lexi_file.readlines()
filenumber = filenumber + 1

except(IOError):
print "Something went wrong trying to read the file:"
print "'" + str(sys.path[0]) + files + "'"



But that was not very sucksessfully. I am not even sure on how to define an empty 2D list. Thanks for all help.

ØØ
 
K

Klaus Alexander Seistrup

Øyvind Østlund said:
I have a list of about 20 files that I want to read line by
line into a 2D list. So the first dimension will be each file,
and the second every line in that file.

I tried to do something like this:

files_and_lines = [][]
filenumber = 0

for files in file_names:
try:
lexi_file = open(str(sys.path[0]) + "/lexi/" + files, "r")
files_and_lines[filenumber] = lexi_file.readlines()
filenumber = filenumber + 1

except(IOError):
print "Something went wrong trying to read the file:"
print "'" + str(sys.path[0]) + files + "'"

I'm not sure I understand you. Do you wish to end up with an array
like this:

#v+

[fileName0][fileLines0]
[fileName1][fileLines1]
...
[fileNameN][fileLinesN]

#v-

In that case try something like:

#v+
files_and_lines = []
for name in file_names:
files_and_lines.append([name, open(name, 'r').readlines()])
print 'Read %d files' % (len(files_and_lines),)

#v-

Add proper error checking.

At least, I think the [].append() method is what you're looking for.

Cheers,
 
L

Larry Bates

Few observations.

1) Don't concatenate pathnames yourself use os.path.join, that
makes your code portable.

lexi_file = open(os.path.join(sys.path[0],"lexi",files), "r")

2) Start with an empty list and append your "lines" lists:

files_and_lines=[]
filenumber = 0

for files in file_names:
fpath=os.path.join(sys.path[0]),"lexi", files)
try:
lexi_file = open(fpath, "r")
files_and_lines.append(lexi_file.readlines())

except(IOError):
print "Something went wrong trying to read the file:"
print "'%s'" % fpath

lexi_file.close() # Remember to close each file

Now you have:

files_and_lines[0][0] -> Line 1 of the first file
files_and_lines[0][1] -> Line 2 of the first file
..
..
..
files_and_lines[0][n] -> Line n of the first file
files_and_lines[1][0] -> Line 1 of the second file
..
..
..

Larry Bates
 
L

Larry Bates

Few observations.

1) Don't concatenate pathnames yourself use os.path.join, that
makes your code portable.

lexi_file = open(os.path.join(sys.path[0],"lexi",files), "r")

2) Start with an empty list and append your "lines" lists:

files_and_lines=[]
filenumber = 0

for files in file_names:
fpath=os.path.join(sys.path[0]),"lexi", files)
try:
lexi_file = open(fpath, "r")
files_and_lines.append(lexi_file.readlines())

except(IOError):
print "Something went wrong trying to read the file:"
print "'%s'" % fpath

lexi_file.close() # Remember to close each file

Now you have:

files_and_lines[0][0] -> Line 1 of the first file
files_and_lines[0][1] -> Line 2 of the first file
..
..
..
files_and_lines[0][n] -> Line n of the first file
files_and_lines[1][0] -> Line 1 of the second file
..
..
..

Larry Bates
 
D

Dennis Lee Bieber

I have a list of about 20 files that I want to read line by line into a 2D list. So the first dimension will be each file, and the second every line in that file.

A bit confusing but... UNTESTED -- I'm coding off the top of my
head.

theData = []

for i in len(file_names):
theData = None #init placeholder/bad file
fid = os.path.join(sys.path[0], "lexi", filenames)
try:
fin = open(fid, "r")
theData = fin.readlines()
fin.close()
except (IOError):
print "An error occurred opening/reading file: ", fid

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top