[Tutor] Help Please

2019-02-20 Thread Mario Ontiveros
Hello,
I am new to python and have been stuck on this for a while. What I am 
trying to do is to remove rows with void, disconnected, and error on lines. The 
code I have does that, the only problem is that it removes my header because 
void is in header. I need to keep header.

Any help will be greatly appreciated.

with open("PSS.csv","r+") as f:
new_f = f.readlines()
f.seek(0)
for line in new_f:
if "Void" not in line:
if "Disconnected" not in line:
if "Error" not in line:
 f.write(line)
f.truncate()



Mario Ontiveros


---
Confidentiality Warning: This message and any attachments are intended only for 
the 
use of the intended recipient(s), are confidential, and may be privileged. If 
you are 
not the intended recipient, you are hereby notified that any review, 
retransmission, 
conversion to hard copy, copying, circulation or other use of all or any 
portion of 
this message and any attachments is strictly prohibited. If you are not the 
intended 
recipient, please notify the sender immediately by return e-mail, and delete 
this 
message and any attachments from your system.
---
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help Please

2019-02-20 Thread Mark Lawrence

On 20/02/2019 14:30, Mario Ontiveros wrote:

Hello,
 I am new to python and have been stuck on this for a while. What I am 
trying to do is to remove rows with void, disconnected, and error on lines. The 
code I have does that, the only problem is that it removes my header because 
void is in header. I need to keep header.

Any help will be greatly appreciated.

with open("PSS.csv","r+") as f:
 new_f = f.readlines()
 f.seek(0)
 for line in new_f:
 if "Void" not in line:
 if "Disconnected" not in line:
 if "Error" not in line:
  f.write(line)
 f.truncate()



Mario Ontiveros



Something like (completely from memory so untested) :-

with open("PSS.csv","r") as inf:
   lines = inf.readlines()

with open("PSS.csv","w") as outf:
   fiter = iter(lines)
   line = next(fiter)
   outf.write(line)
   for line in fiter:
  if "Void" not in line and "Disconnected" not in line and "Error" 
not in line: # probably a simpler way of writing this but I'm knackered :-)

   outf.write(line)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Re: [Tutor] Help Please

2019-02-20 Thread Alan Gauld via Tutor
On 20/02/2019 14:30, Mario Ontiveros wrote:
> Hello,
> I am new to python and have been stuck on this for a while. What I am 
> trying to do is to remove rows with void, disconnected, and error on lines. 
> The code I have does that, the only problem is that it removes my header 
> because void is in header. I need to keep header.
> 
> Any help will be greatly appreciated.
> 
> with open("PSS.csv","r+") as f:
> new_f = f.readlines()
> f.seek(0)
> for line in new_f:

I don't know how long your header is but assuming it is
only 1 line you can simply use slicing to remove the first
line:

  for line in new_f[1:]:

If the header is multi line simply change to the first line you need to
process, for example to remove 3 lines use new_f[3:]

> if "Void" not in line:
> if "Disconnected" not in line:
> if "Error" not in line:
>f.write(line)

This only writes the line if all three terms are present. Assuming thats
what you want it might be more obviously written as

if ("Void" in line and
   "Disconnected in line and
   "Error" in line):
  f.write(line)

You could also use a regex to search for all three and if its
a long file that will probably be faster since it only traverses
the list once. The snag is the regex gets complex if you need
all three in any order. But if you know the order in which
the terms arise it's probably the best option.

> f.truncate()

While overwriting the original file works, it's slightly dangerous
in that you lose the original data if anything goes wrong.
The more usual way to do things is to create a new file for writing
then rename it to the original if, and only if, everything works.
You might even rename the original to .bak first to be really safe.

The other advantage of this approach is that you don't need the
readlines)() call but can just process the file line by line
directly which should also be faster.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] Help Please

2019-02-20 Thread Alex Kleider

On 2019-02-20 06:30, Mario Ontiveros wrote:

Hello,
I am new to python and have been stuck on this for a while. What I
am trying to do is to remove rows with void, disconnected, and error
on lines. The code I have does that, the only problem is that it
removes my header because void is in header. I need to keep header.

Any help will be greatly appreciated.

with open("PSS.csv","r+") as f:
new_f = f.readlines()
f.seek(0)
for line in new_f:
if "Void" not in line:
if "Disconnected" not in line:
if "Error" not in line:
 f.write(line)
f.truncate()



Mario Ontiveros


Since your file seems to be a csv file, can we assume your 'header' line 
is really a comma separated list of column names?


If so, then using the csv module and specifically csv.DictReader (+/- 
DictWriter) might make things easier for you.

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


Re: [Tutor] Recommended Resurce or strategy for beginning students

2019-02-20 Thread David
On Thu, 21 Feb 2019 at 14:35, Matthew Polack
 wrote:
>
> Just wanted to update this thread regarding a resource for beginning 
> students. We are now 4 weeks into the course and have found ...

Hi Matthew,

Thanks for sharing your progress here! I'm very pleased to hear
you are finding classroom resources that work for you.

I can't resist to throw another left-field link at you, it is not
directly related
to anything you mentioned, but simply in case you or anyone else finds it
interesting.

micropython ... I just discovered it myself recently, here is a video
presentation by its creator:
https://www.youtube.com/watch?v=Zm08hXeuv-I

It is worth watching from the beginning, but there is specific mention
of its use in UK education around time 39:00.

Personally I think that it's very cool to be able to control hardware with
a Python REPL interface.

If you search the web for micropython you will find more resources,
as I currently doing myself, for a specific hardware project.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor