[Tutor] Suggestions Please

Dave Angel davea at davea.name
Wed Oct 8 10:38:18 CEST 2014


Phillip Pugh <pughpl at me.com> Wrote in message:
> Thank you All!!  
> 
> I am impressed with the support. It was very helpful and timely.  I was able to put together a script to do what I wanted. I know now that I wont be wasting time learning Python.  As with any language, it is about understanding the syntax. As I mentioned before, I want to make sure I am focusing my time on something useful and this was a big help.  
> 
> Here is what I came up with (with your help) . I expect there is a more efficient way to do it, but hey... it was my first try with data.  And FYI, I work with over one hundred data sources, I wanted to test on something small.
> 
> Phillip 
> 
> 
> with open("InputTest.txt","r") as f:
>     with open("Outputt.txt", "w") as fw:  
>         for line in f:
>             first,second = line[:32], line[32:37]
>        
>             if first.isspace()== False:
>                 fw.write (second.strip()+ first.strip()+"\n")    
>  
> f.close()
> fw.close()
> 
> 
> 

Some comments,  first the minor stuff.

No need to close f or fw; the with statements already took care of
 it before you got to those lines. And the if statement would
 normally be spelled
   if not first.isspace ():

But the important thing is that by stripping those two strings, 
 you're almost certainly damaging the output data. Unless you know
 some specific reason why you can get away with it.

Also, please don't top-post.

> 
> On Oct 7, 2014, at 4:39 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> 
>> On 06/10/14 23:42, Phillip Pugh wrote:
>>> I am trying to decide if Python is the right toolset for me.
>> > I do a lot of data analytics.
>
>> 
>>> move "structured" data around and then write it to a new file.
>> 
>> What is structured about it? Fixed column width?
>> Fixed relative position? Binary format?
>> 
>>> The txt file has several data elements and is
>> > 300 characters per line.
>> 
>>> I am only interested in the first two fields.
>> > The first data element is 19 characters.
>> > The second data element is 6 characters.
>> 

>> 
>>> I want to rearrange the data by moving the 6 characters data
>> > in front of the 19 characters data
>> 
>> Do you need a separator?
>> 
>>> and then write the 25 character data to a new file.



-- 
DaveA



More information about the Tutor mailing list