File traversing

2012-04-15 Thread Nibin V M
Hello,

First of all..I am very new to python with no background in development
area! :)

Ok, here is my problem.I have opened a file and I need to check each
line of that file. I have done it with a while loop.

res_own_file = open('/bah')
res_own_list = res_own_file.readline()
res_tot_list=[]
while res_own_list:
  res_own_list=res_own_list.strip()
  res_own_list=res_own_list.replace(' ', '')
  res_name=res_own_list.split(':')
  if res_name[1:2] not in res_tot_list:
res_tot_list.append(res_name[1:2])
  res_own_list = res_own_file.readline()

As you can see above, I am reading each line, cut a particular field and
sort a unique list from it...I have two questions here

1. If I have to check the file again from beginning, I can't do it without
closing and re-opening file since the file pointer is assigned to the EOF
of the opened file right? If so, is there any alliterative method to do it?
I don't wish to mess the script with multiple file open-close code!

2. If I print  res_name, it will display like ['one', 'two']. If I
print  res_name[0], it will display one ; but if I print  res_name[1] it
will display error out of index instead of two. From my code, when I use
res_name[1:2] it's displaying second filed. Why it is behaving like this?

Thanks in advance!

Regards

Nibin.

http://TechsWare.in
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File traversing

2012-04-16 Thread Nibin V M
Thank you Chris :) ..I will check it...

On Sun, Apr 15, 2012 at 3:21 PM, Chris Angelico  wrote:

> On Sun, Apr 15, 2012 at 7:15 PM, Nibin V M  wrote:
> > res_own_file = open('/bah')
> > res_own_list = res_own_file.readline()
> > res_tot_list=[]
> > while res_own_list:
> >   res_own_list=res_own_list.strip()
> >   res_own_list=res_own_list.replace(' ', '')
> >   res_name=res_own_list.split(':')
> >   if res_name[1:2] not in res_tot_list:
> > res_tot_list.append(res_name[1:2])
> >   res_own_list = res_own_file.readline()
>
> You can iterate over the file thus:
>
> for res_own_list in res_own_file:
>  # body of loop here
>
> That saves you the trouble of making sure you do the next readline at
> the bottom, too.
>
> (You may also want to consider using the 'with' statement to guarantee
> a timely closing of the file. Outside the scope of this mail though.)
>
> > As you can see above, I am reading each line, cut a particular field and
> > sort a unique list from it...I have two questions here
> >
> > 1. If I have to check the file again from beginning, I can't do it
> without
> > closing and re-opening file since the file pointer is assigned to the
> EOF of
> > the opened file right? If so, is there any alliterative method to do it?
> I
> > don't wish to mess the script with multiple file open-close code!
>
> You should be able to use res_own_file.seek(0) to do that. I haven't
> tested your code, but that ought to work. If it fails, post code that
> uses seek and the error message you get, and someone will doubtless
> know what's wrong.
>
> > 2. If I print  res_name, it will display like ['one', 'two']. If I
> > print  res_name[0], it will display one ; but if I print  res_name[1] it
> > will display error out of index instead of two. From my code, when I use
> > res_name[1:2] it's displaying second filed. Why it is behaving like this?
>
> My guess here is that at the end of the file, you get a blank line.
> When you use [1:2] syntax, you get back an empty list if the indices
> are out of bounds; but [1] will throw an error.
>
> if res_name[1:2] not in res_tot_list:
>res_tot_list.append(res_name[1:2])
>
> I think this list is just to collect unique entries, yes? If so, a set
> may be more to your liking. Check out:
> http://docs.python.org/py3k/library/stdtypes.html#set
>
> Hope that's of some value!
>
> Chris Angelico
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Regards

Nibin.

http://TechsWare.in
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File traversing

2012-04-17 Thread Nibin V M
wow...thanks Karl :)

On Tue, Apr 17, 2012 at 2:20 AM, Karl Knechtel  wrote:

>
>
> On Sun, Apr 15, 2012 at 5:51 AM, Chris Angelico  wrote:
>
>>
>> (You may also want to consider using the 'with' statement to guarantee
>> a timely closing of the file. Outside the scope of this mail though.)
>>
>> I think this list is just to collect unique entries, yes? If so, a set
>> may be more to your liking. Check out:
>> http://docs.python.org/py3k/library/stdtypes.html#set
>>
>>  
>>
>
> In cases like this I like to just show the final code after making all the
> changes, and let the student ask questions :)
>
> with open('/bah') as res_own_file:
> all_res = set(line.strip().replace(' ', '').split(':')[1] for line in
> res_own_file if ':' in line)
>
> --
> ~Zahlman {:>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


-- 
Regards

Nibin.

http://TechsWare.in
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File traversing

2012-04-17 Thread Nibin V M
# python test.py
  File "test.py", line 1
with open('/etc/trueuserowners') as res_own_file:
^
IndentationError: unexpected indent
 [~]# cat test.py
 with open('/etc/trueuserowners') as res_own_file:
all_res = set(line.strip().replace(' ', '').split(':')[1] for line in
res_own_file if ':' in line)

 print all_res

am I missing something? :)

On Tue, Apr 17, 2012 at 4:44 PM, Nibin V M  wrote:

> wow...thanks Karl :)
>
> On Tue, Apr 17, 2012 at 2:20 AM, Karl Knechtel  wrote:
>
>>
>>
>> On Sun, Apr 15, 2012 at 5:51 AM, Chris Angelico  wrote:
>>
>>>
>>> (You may also want to consider using the 'with' statement to guarantee
>>> a timely closing of the file. Outside the scope of this mail though.)
>>>
>>> I think this list is just to collect unique entries, yes? If so, a set
>>> may be more to your liking. Check out:
>>> http://docs.python.org/py3k/library/stdtypes.html#set
>>>
>>>  <http://mail.python.org/mailman/listinfo/python-list>
>>>
>>
>> In cases like this I like to just show the final code after making all
>> the changes, and let the student ask questions :)
>>
>> with open('/bah') as res_own_file:
>> all_res = set(line.strip().replace(' ', '').split(':')[1] for line in
>> res_own_file if ':' in line)
>>
>> --
>> ~Zahlman {:>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>>
>
>
> --
> Regards
>
> Nibin.
>
> http://TechsWare.in
>
>


-- 
Regards

Nibin.

http://TechsWare.in
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File traversing

2012-04-17 Thread Nibin V M
thanks for a super fast reply Chris :)



On Tue, Apr 17, 2012 at 8:06 PM, Chris Angelico  wrote:

> On Wed, Apr 18, 2012 at 12:31 AM, Nibin V M  wrote:
> > # python test.py
> >   File "test.py", line 1
> > with open('/etc/trueuserowners') as res_own_file:
> > ^
> > IndentationError: unexpected indent
>
> Make sure your first code line is flush left. Since indentation
> indicates block structure (and not scoping, as it does in C-like
> languages), Python refuses to allow it anywhere other than inside
> something that can make use of it. If you're copying and pasting a
> huge lot of code from somewhere and it's all indented a minimum of a
> couple of spaces, the simplest solution may be to add a line at the
> top saying "if True:", thus making your whole program into a massive
> block if, which Python will happily accept.
>
> ChrisA
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Regards

Nibin.

http://TechsWare.in
-- 
http://mail.python.org/mailman/listinfo/python-list


python and xml

2012-05-16 Thread Nibin V M
Hi,

I am trying to use cPanel XML-API and every API call return data in XML
format. I would like to know how to manipulate the data here.

For eg: How can I read the CPU load data from the below output


 0.000.000.00

Thank you,
-- 
Regards

Nibin.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python and xml

2012-05-16 Thread Nibin V M
thank you Stefan. but the XML output is assigned to a variable; how to
process the variable with XML contents?

On Wed, May 16, 2012 at 7:53 PM, Stefan Behnel  wrote:

> Nibin V M, 16.05.2012 16:16:
> > I am trying to use cPanel XML-API and every API call return data in XML
> > format. I would like to know how to manipulate the data here.
> >
> > For eg: How can I read the CPU load data from the below output
> >
> >
> >
>  
> 0.000.000.00
>
> Here's some untested code to print the text values:
>
>  import xml.etree.ElementTree as ET
>
>  loadavg = ET.fromstring('0.000.00'
>  '0.00')
>
>  for interval_tag in ('one', 'five', 'fifteen'):
>  print(loadavg.findtext(interval_tag))
>
>
> Stefan
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Regards

Nibin.

http://TechsWare.in
-- 
http://mail.python.org/mailman/listinfo/python-list


print XML

2012-05-17 Thread Nibin V M
Hello,

I have the following code, which will assign  XML data to a variable! What
is the best method to write the contents of the variable to a file?

===
doc = minidom.parse(sys.stdin)
===

Any help will be highly appreciated!

Thank you,
-- 
Regards

Nibin.

http://TechsWare.in
-- 
http://mail.python.org/mailman/listinfo/python-list