<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: PythonWin2.6.5:open a text file, read it into memory, and then print each line-fa in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/pythonwin2-6-5-open-a-text-file-read-it-into/m-p/173569#M13390</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;See the attached docx file for the actual page of executing Python code on Python 2.6.5.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Scott Chang&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 06 Nov 2012 19:32:17 GMT</pubDate>
    <dc:creator>ScottChang</dc:creator>
    <dc:date>2012-11-06T19:32:17Z</dc:date>
    <item>
      <title>PythonWin2.6.5:open a text file, read it into memory, and then print each line-fails</title>
      <link>https://community.esri.com/t5/python-questions/pythonwin2-6-5-open-a-text-file-read-it-into/m-p/173568#M13389</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In Python Win 2.6.5, I executed the following Python script on the attached txt file:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# 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
#&amp;nbsp;&amp;nbsp; position of each data column
for line in file:
&amp;nbsp;&amp;nbsp;&amp;nbsp; v0 = line[0:3]
&amp;nbsp;&amp;nbsp;&amp;nbsp; v1 = line[3:18].strip()
&amp;nbsp;&amp;nbsp;&amp;nbsp; v2 = line[18:19]
&amp;nbsp;&amp;nbsp;&amp;nbsp; v3 = line[19:20]
&amp;nbsp;&amp;nbsp;&amp;nbsp; v4 = line[20:21]
&amp;nbsp;&amp;nbsp;&amp;nbsp; v5 = line[21:26]
&amp;nbsp;&amp;nbsp;&amp;nbsp; v6 = line[26:27]
&amp;nbsp;&amp;nbsp;&amp;nbsp; v7 = line[27:29]
&amp;nbsp;&amp;nbsp;&amp;nbsp; # add the parsed items to list l; as we go through the lines,
&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; we create a nested list
&amp;nbsp;&amp;nbsp;&amp;nbsp; l.extend([[v0,v1,v2,v3,v4,v5,v6,v7]])
# iterate through list, print out members
for each in l:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print each&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It does not work at all - no error and no output.&amp;nbsp; Please kindly help and advise me how to fix the problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Scott Chang&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 06 Nov 2012 19:20:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pythonwin2-6-5-open-a-text-file-read-it-into/m-p/173568#M13389</guid>
      <dc:creator>ScottChang</dc:creator>
      <dc:date>2012-11-06T19:20:53Z</dc:date>
    </item>
    <item>
      <title>Re: PythonWin2.6.5:open a text file, read it into memory, and then print each line-fa</title>
      <link>https://community.esri.com/t5/python-questions/pythonwin2-6-5-open-a-text-file-read-it-into/m-p/173569#M13390</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;See the attached docx file for the actual page of executing Python code on Python 2.6.5.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Scott Chang&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 06 Nov 2012 19:32:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pythonwin2-6-5-open-a-text-file-read-it-into/m-p/173569#M13390</guid>
      <dc:creator>ScottChang</dc:creator>
      <dc:date>2012-11-06T19:32:17Z</dc:date>
    </item>
    <item>
      <title>Re: PythonWin2.6.5:open a text file, read it into memory, and then print each line-fa</title>
      <link>https://community.esri.com/t5/python-questions/pythonwin2-6-5-open-a-text-file-read-it-into/m-p/173570#M13391</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Your script works on my machine perfectly in Pythonwin.&amp;nbsp; But I'm running it as a script.&amp;nbsp; From your Word file,&amp;nbsp; you seem to be running it line by line in the interactive window.&amp;nbsp; Try running as a script.&amp;nbsp; Here's the output from my Pythonwin stdout:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PythonWin 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;gt;&amp;gt;&amp;gt; ['056', '10616', '1', '6', '1', '00300', '0', '09']&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;['056', '10617', '1', '6', '1', '00300', '0', '09']&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;['056', '000000000000263', '1', '2', '1', '00071', '0', '03']&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;['056', '000000000000264', '1', '2', '1', '00071', '0', '03']&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;['056', '000000000000279', '1', '4', '1', '00721', '0', '03']&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;['056', '000000000000284', '1', '2', '1', '00070', '0', '03']&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;['056', '000000000000286', '1', '2', '1', '00070', '0', '03']&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;['056', '000000000000305', '1', '2', '6', '00167', '0', '07']&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;['056', '000000000000306', '1', '2', '1', '00167', '0', '07']&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;['056', '000000000000307', '1', '2', '1', '00167', '0', '07']&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;good luck,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 06 Nov 2012 22:42:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pythonwin2-6-5-open-a-text-file-read-it-into/m-p/173570#M13391</guid>
      <dc:creator>MikeHunter</dc:creator>
      <dc:date>2012-11-06T22:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: PythonWin2.6.5:open a text file, read it into memory, and then print each line-fa</title>
      <link>https://community.esri.com/t5/python-questions/pythonwin2-6-5-open-a-text-file-read-it-into/m-p/173571#M13392</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Mike, Thanks for your nice response.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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,....."?&amp;nbsp; Please answer this question for me.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2) From &lt;/SPAN&gt;&lt;A href="http://docs.python.org/2/library/csv.html"&gt;http://docs.python.org/2/library/csv.html&lt;/A&gt;&lt;SPAN&gt;, I see "CSV File Reading and Writing" that is hard to learn and use.&amp;nbsp; Do you know where I can find the tutorial examples for doing the&amp;nbsp; "CSV File Reading and Writing" Python programming?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3) I have not found "TXT File Reading and Writing" in the &lt;/SPAN&gt;&lt;A href="http://docs.python.org"&gt;http://docs.python.org&lt;/A&gt;&lt;SPAN&gt; place!!&amp;nbsp; Do you know where I can find the Python "TXT File Reading and Writing" and their tutorial examples for practice?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please respond again.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Scott Chang&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Nov 2012 11:22:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pythonwin2-6-5-open-a-text-file-read-it-into/m-p/173571#M13392</guid>
      <dc:creator>ScottChang</dc:creator>
      <dc:date>2012-11-07T11:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: PythonWin2.6.5:open a text file, read it into memory, and then print each line-fa</title>
      <link>https://community.esri.com/t5/python-questions/pythonwin2-6-5-open-a-text-file-read-it-into/m-p/173572#M13393</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here's a quick breakdown what the script is doing:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The "for line in file" part iterates through the lines of the text file, and assigns the name "line" to each line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The lines like v0 = line[0:3] use Python slice notation to break up the line into pieces.&amp;nbsp; This first one assigns the first 3 characters to the name v0.&amp;nbsp; 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.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here are several good examples of using slice notation:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation"&gt;http://stackoverflow.com/questions/509211/good-primer-for-python-slice-notation&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As far as the csv module goes, I find it very easy to use, and I highly recommend it for dealing with csv files.&amp;nbsp; You can run into a bunch of tricky situations with csv's, and the csv module will handle these for you.&amp;nbsp; Plus it's a good practice to use the standard library and not try to handle things that are already handled for you there.&amp;nbsp; I don't know of a better documentation than the official one.&amp;nbsp; But you can try Doug Hellmann's excellent site:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://www.doughellmann.com/PyMOTW/csv/index.html#module-csv"&gt;http://www.doughellmann.com/PyMOTW/csv/index.html#module-csv&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Dealing with text files is really easy in Python.&amp;nbsp; Read about file objects here:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/2/library/stdtypes.html#file-objects"&gt;http://docs.python.org/2/library/stdtypes.html#file-objects&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And about the opening, reading, and writing text and other kinds of files here:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files"&gt;http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;hope this helps,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Nov 2012 23:24:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/pythonwin2-6-5-open-a-text-file-read-it-into/m-p/173572#M13393</guid>
      <dc:creator>MikeHunter</dc:creator>
      <dc:date>2012-11-08T23:24:02Z</dc:date>
    </item>
  </channel>
</rss>

