<?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 Why is my IF statement being ignored? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/why-is-my-if-statement-being-ignored/m-p/303220#M23583</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm sure there is a simple answer, but it seems to be beyond my limited Python knowledge. I have some very large text files, in which data is arranged in columns. One of the attributes is 'Month' and I want to create a new txt file for each month. My code is as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;infile = open(r"P:\Months_Test.txt")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;mth = 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for inlines in infile.readlines():&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if str(mth) in inlines[-2:]:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile = open(r"P:\Month_" + str(mth) + ".txt", 'a')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.write(inlines)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.close()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mth += 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;infile.close()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I run this code without the ELSE statement, it correctly outputs the month 1 data into the month 1 text file. However, as soon as I add the ELSE statement, it skips the IF statement completely and just counts the number of lines in the text file (I checked by printing mth). What am I doing wrong?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 01 Mar 2011 09:38:05 GMT</pubDate>
    <dc:creator>LeeMusto</dc:creator>
    <dc:date>2011-03-01T09:38:05Z</dc:date>
    <item>
      <title>Why is my IF statement being ignored?</title>
      <link>https://community.esri.com/t5/python-questions/why-is-my-if-statement-being-ignored/m-p/303220#M23583</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm sure there is a simple answer, but it seems to be beyond my limited Python knowledge. I have some very large text files, in which data is arranged in columns. One of the attributes is 'Month' and I want to create a new txt file for each month. My code is as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;infile = open(r"P:\Months_Test.txt")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;mth = 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for inlines in infile.readlines():&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if str(mth) in inlines[-2:]:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile = open(r"P:\Month_" + str(mth) + ".txt", 'a')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.write(inlines)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outfile.close()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mth += 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;infile.close()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I run this code without the ELSE statement, it correctly outputs the month 1 data into the month 1 text file. However, as soon as I add the ELSE statement, it skips the IF statement completely and just counts the number of lines in the text file (I checked by printing mth). What am I doing wrong?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Mar 2011 09:38:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-is-my-if-statement-being-ignored/m-p/303220#M23583</guid>
      <dc:creator>LeeMusto</dc:creator>
      <dc:date>2011-03-01T09:38:05Z</dc:date>
    </item>
    <item>
      <title>Re: Why is my IF statement being ignored?</title>
      <link>https://community.esri.com/t5/python-questions/why-is-my-if-statement-being-ignored/m-p/303221#M23584</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;OK - just realised I should use 'For mth in range(1,13):' to solve problem. But, would still like to know why the IF statement wasn't working.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, why does this work:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;'if str(mth) in inlines[-2:]:'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;but, this doesn't:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;'if str(mth) == inlines[-2:]' (Returns nothing)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The 'in' function will work if there are only 9 months, but runs into trouble for months 10-12.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Mar 2011 10:18:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-is-my-if-statement-being-ignored/m-p/303221#M23584</guid>
      <dc:creator>LeeMusto</dc:creator>
      <dc:date>2011-03-01T10:18:54Z</dc:date>
    </item>
    <item>
      <title>Re: Why is my IF statement being ignored?</title>
      <link>https://community.esri.com/t5/python-questions/why-is-my-if-statement-being-ignored/m-p/303222#M23585</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;For pure Python questions I recommend StackOverflow.com. You will generally get answers within minutes of asking a question. This forum is more for ArcGIS-specific Python usage and is far less trafficked than SO.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In regards to your question I can only suggest to step through it in a debugger or sprinkle it with print statements so you are aware of the flow of control and variable contents at any point during execution.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Mar 2011 15:52:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-is-my-if-statement-being-ignored/m-p/303222#M23585</guid>
      <dc:creator>LoganPugh</dc:creator>
      <dc:date>2011-03-01T15:52:30Z</dc:date>
    </item>
    <item>
      <title>Re: Why is my IF statement being ignored?</title>
      <link>https://community.esri.com/t5/python-questions/why-is-my-if-statement-being-ignored/m-p/303223#M23586</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;When you iterate over a file, each line gives you the final return at the end, you'll need to do something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;if str(mth) in inlines.rstrip()[-2:]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Otherwise line[-2:] is equal to "\r\n" and not your number (which is at [-4:-2])&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 05 Mar 2011 21:59:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-is-my-if-statement-being-ignored/m-p/303223#M23586</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2011-03-05T21:59:04Z</dc:date>
    </item>
  </channel>
</rss>

