Re: [Tutor] odd behavior when renaming a file

2012-05-10 Thread Joel Goldstick
On Thu, May 10, 2012 at 5:05 PM, Prasad, Ramit
 wrote:
>> I have to process a csv file from a business partner.  Oddly (?) they
>> don't quote text fields, and the Title field sometimes contains
>> commas.  So I wrote some code to count the commas in each line and if
>> there were too many, I removed the extras and wrote the cleaned up
>> file to the original filename for the rest of what I have to with that
>> data
>
> That is terrible (of them). How do you determine which comma is the "extra"?

Yes, it is kinda disheartening, but I don't know if they distribute
this file to others, and changing it might have ramifications.

I take the line and split it on commas.  Then get the length of the
list.  If its greater than 8, I append the item in the list that is
after the Title field (I don't have the code with me -- i think its
the 6th item) to the field that is the beginning of the title.  Then I
remove (pop) that field.  Lather, rinse, repeat until there are 8
elements in the list.  Then I join the list back with commas and write
to the output file.

>
>
> Ramit
>
>
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
>
> --
>
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] odd behavior when renaming a file

2012-05-10 Thread Prasad, Ramit
> I have to process a csv file from a business partner.  Oddly (?) they
> don't quote text fields, and the Title field sometimes contains
> commas.  So I wrote some code to count the commas in each line and if
> there were too many, I removed the extras and wrote the cleaned up
> file to the original filename for the rest of what I have to with that
> data

That is terrible (of them). How do you determine which comma is the "extra"?


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] odd behavior when renaming a file

2012-05-10 Thread Alan Gauld

On 10/05/12 21:18, Dave Angel wrote:


   out_file = open('revelex.csv', 'w')
   # etc.

I would expect the open() to fail...


But he's opening it for WRITE, so it gets created just fine.


Ah yes, I didn't spot that. :-)
Too busy looking for a possible cause of a missing file message...


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] odd behavior when renaming a file

2012-05-10 Thread Joel Goldstick
On Thu, May 10, 2012 at 4:18 PM, Dave Angel  wrote:
> On 05/10/2012 12:56 PM, Alan Gauld wrote:
>> On 09/05/12 20:26, Joel Goldstick wrote:
>>> import os
>>> def pre_process():
>>>      if os.path.isfile('revelex.csv'):
>>>          os.rename('revelex.csv', 'revelex.tmp')
>>>          print "Renamed ok"
>>>      else:
>>>          print "Exiting, no revelex.csv file available"
>>>          exit()
>>>      out_file = open('revelex.csv', 'w')
>>>      # etc.
>>
>>> When I run the code above it works file if run from the file.  But
>>> when I import it and run it from another file it renames the file but
>>> then prints "Exiting, no revelex.csv file available"
>>
>> I don;t know the reason but are you sure you want to open the file
>> that you have just renamed?
>>
>> def pre_process():
>>       if os.path.isfile('revelex.csv'):
>>           os.rename('revelex.csv', 'revelex.tmp')
>> ...
>>       out_file = open('revelex.csv', 'w')
>>       # etc.
>>
>> I would expect the open() to fail...
>>
>
> But he's opening it for WRITE, so it gets created just fine.
>
>
>
> --
>
> DaveA
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

I have to process a csv file from a business partner.  Oddly (?) they
don't quote text fields, and the Title field sometimes contains
commas.  So I wrote some code to count the commas in each line and if
there were too many, I removed the extras and wrote the cleaned up
file to the original filename for the rest of what I have to with that
data


-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] odd behavior when renaming a file

2012-05-10 Thread Dave Angel
On 05/10/2012 12:56 PM, Alan Gauld wrote:
> On 09/05/12 20:26, Joel Goldstick wrote:
>> import os
>> def pre_process():
>>  if os.path.isfile('revelex.csv'):
>>  os.rename('revelex.csv', 'revelex.tmp')
>>  print "Renamed ok"
>>  else:
>>  print "Exiting, no revelex.csv file available"
>>  exit()
>>  out_file = open('revelex.csv', 'w')
>>  # etc.
>
>> When I run the code above it works file if run from the file.  But
>> when I import it and run it from another file it renames the file but
>> then prints "Exiting, no revelex.csv file available"
>
> I don;t know the reason but are you sure you want to open the file
> that you have just renamed?
>
> def pre_process():
>   if os.path.isfile('revelex.csv'):
>   os.rename('revelex.csv', 'revelex.tmp')
> ...
>   out_file = open('revelex.csv', 'w')
>   # etc.
>
> I would expect the open() to fail...
>

But he's opening it for WRITE, so it gets created just fine.



-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] odd behavior when renaming a file

2012-05-10 Thread Joel Goldstick
On Wed, May 9, 2012 at 5:21 PM, Peter Otten <__pete...@web.de> wrote:
> Joel Goldstick wrote:
>
>> import os
>> def pre_process():
>>     if os.path.isfile('revelex.csv'):
>>         os.rename('revelex.csv', 'revelex.tmp')
>>         print "Renamed ok"
>>     else:
>>         print "Exiting, no revelex.csv file available"
>>         exit()
>>     out_file = open('revelex.csv', 'w')
>>     # etc.
>>
>> if __name__ == '__main__':
>>     pre_process()
>>
>>
>> When I run the code above it works file if run from the file.  But
>> when I import it and run it from another file it renames the file but
>> then prints "Exiting, no revelex.csv file available"
>
> Add
>
> print os.getcwd()
>
> to your code, you are probably in the wrong directory.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Thanks for the hint.  I am in the right directory, but I failed to
notice that I renamed the file before I entered my function.
Funny how a night of sleep can make dumb mistakes pop out.

-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] odd behavior when renaming a file

2012-05-10 Thread Alan Gauld

On 09/05/12 20:26, Joel Goldstick wrote:

import os
def pre_process():
 if os.path.isfile('revelex.csv'):
 os.rename('revelex.csv', 'revelex.tmp')
 print "Renamed ok"
 else:
 print "Exiting, no revelex.csv file available"
 exit()
 out_file = open('revelex.csv', 'w')
 # etc.



When I run the code above it works file if run from the file.  But
when I import it and run it from another file it renames the file but
then prints "Exiting, no revelex.csv file available"


I don;t know the reason but are you sure you want to open the file that 
you have just renamed?


def pre_process():
  if os.path.isfile('revelex.csv'):
  os.rename('revelex.csv', 'revelex.tmp')
...
  out_file = open('revelex.csv', 'w')
  # etc.

I would expect the open() to fail...

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] odd behavior when renaming a file

2012-05-09 Thread Peter Otten
Joel Goldstick wrote:

> import os
> def pre_process():
> if os.path.isfile('revelex.csv'):
> os.rename('revelex.csv', 'revelex.tmp')
> print "Renamed ok"
> else:
> print "Exiting, no revelex.csv file available"
> exit()
> out_file = open('revelex.csv', 'w')
> # etc.
> 
> if __name__ == '__main__':
> pre_process()
> 
> 
> When I run the code above it works file if run from the file.  But
> when I import it and run it from another file it renames the file but
> then prints "Exiting, no revelex.csv file available"

Add 

print os.getcwd() 

to your code, you are probably in the wrong directory.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] odd behavior when renaming a file

2012-05-09 Thread Walter Prins
Hi,

On 9 May 2012 20:26, Joel Goldstick  wrote:

> import os
> def pre_process():
>if os.path.isfile('revelex.csv'):
>os.rename('revelex.csv', 'revelex.tmp')
>print "Renamed ok"
>else:
>print "Exiting, no revelex.csv file available"
>exit()
>out_file = open('revelex.csv', 'w')
># etc.
>
> if __name__ == '__main__':
>pre_process()
>
>
> When I run the code above it works file if run from the file.  But
> when I import it and run it from another file it renames the file but
> then prints "Exiting, no revelex.csv file available"
>


Can you post where/how you call this from another file? Anyway, it sounds
like the pre_process() routine is being called twice, somehow.  On the
first call the file is renamed.  Then on the second call, of course the
file is not there anymore (as it's been renamed) and thus it prints the
"Exiting" message.

Best,

Walter
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] odd behavior when renaming a file

2012-05-09 Thread Joel Goldstick
import os
def pre_process():
if os.path.isfile('revelex.csv'):
os.rename('revelex.csv', 'revelex.tmp')
print "Renamed ok"
else:
print "Exiting, no revelex.csv file available"
exit()
out_file = open('revelex.csv', 'w')
# etc.

if __name__ == '__main__':
pre_process()


When I run the code above it works file if run from the file.  But
when I import it and run it from another file it renames the file but
then prints "Exiting, no revelex.csv file available"



-- 
Joel Goldstick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor