Select to view content in your preferred language

PythonWin2.6.5:open a text file, read it into memory, and then print each line-fails

1407
4
11-06-2012 11:20 AM
ScottChang
Deactivated User
Hi all,
In Python Win 2.6.5, I executed the following Python script on the attached txt file:
# open the text file, read it into memory
file = open('c:/testing/a-little-more-involved.txt').read().splitlines()
# define a new, empty list object
l = []
# iterate through the lines of the file, slice up the line by index
#   position of each data column
for line in file:
    v0 = line[0:3]
    v1 = line[3:18].strip()
    v2 = line[18:19]
    v3 = line[19:20]
    v4 = line[20:21]
    v5 = line[21:26]
    v6 = line[26:27]
    v7 = line[27:29]
    # add the parsed items to list l; as we go through the lines,
    #   we create a nested list
    l.extend([[v0,v1,v2,v3,v4,v5,v6,v7]])
# iterate through list, print out members
for each in l:
    print each


It does not work at all - no error and no output.  Please kindly help and advise me how to fix the problem.

Thanks in advance,
Scott Chang
Tags (2)
0 Kudos
4 Replies
ScottChang
Deactivated User
See the attached docx file for the actual page of executing Python code on Python 2.6.5.

Scott Chang
0 Kudos
MikeHunter
Frequent Contributor
Your script works on my machine perfectly in Pythonwin.  But I'm running it as a script.  From your Word file,  you seem to be running it line by line in the interactive window.  Try running as a script.  Here's the output from my Pythonwin stdout:

PythonWin 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
>>> ['056', '10616', '1', '6', '1', '00300', '0', '09']
['056', '10617', '1', '6', '1', '00300', '0', '09']
['056', '000000000000263', '1', '2', '1', '00071', '0', '03']
['056', '000000000000264', '1', '2', '1', '00071', '0', '03']
['056', '000000000000279', '1', '4', '1', '00721', '0', '03']
['056', '000000000000284', '1', '2', '1', '00070', '0', '03']
['056', '000000000000286', '1', '2', '1', '00070', '0', '03']
['056', '000000000000305', '1', '2', '6', '00167', '0', '07']
['056', '000000000000306', '1', '2', '1', '00167', '0', '07']
['056', '000000000000307', '1', '2', '1', '00167', '0', '07']

good luck,
Mike
0 Kudos
ScottChang
Deactivated User
Hi Mike, Thanks for your nice response.

1) I copied the Python code from someone's file to learn how to read the text file into memory, etc. What are v0, v1, v3,....,v7 in the code statement "for line in file: v0 = line[0:30,....."?  Please answer this question for me.

2) From http://docs.python.org/2/library/csv.html, I see "CSV File Reading and Writing" that is hard to learn and use.  Do you know where I can find the tutorial examples for doing the  "CSV File Reading and Writing" Python programming?

3) I have not found "TXT File Reading and Writing" in the http://docs.python.org place!!  Do you know where I can find the Python "TXT File Reading and Writing" and their tutorial examples for practice?

Please respond again.

Thanks,
Scott Chang
0 Kudos
MikeHunter
Frequent Contributor
Here's a quick breakdown what the script is doing:

The "for line in file" part iterates through the lines of the text file, and assigns the name "line" to each line.
The lines like v0 = line[0:3] use Python slice notation to break up the line into pieces.  This first one assigns the first 3 characters to the name v0.  After all the slices of the line are gathered, then are appended to the list "l" as a list containing the pieces, so you end up with a bunch of lists inside a "master" list. 

Here are several good examples of using slice notation:

http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation

As far as the csv module goes, I find it very easy to use, and I highly recommend it for dealing with csv files.  You can run into a bunch of tricky situations with csv's, and the csv module will handle these for you.  Plus it's a good practice to use the standard library and not try to handle things that are already handled for you there.  I don't know of a better documentation than the official one.  But you can try Doug Hellmann's excellent site:

http://www.doughellmann.com/PyMOTW/csv/index.html#module-csv

Dealing with text files is really easy in Python.  Read about file objects here:

http://docs.python.org/2/library/stdtypes.html#file-objects

And about the opening, reading, and writing text and other kinds of files here:

http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

hope this helps,
Mike
0 Kudos