Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104262 > unrolled thread
| Started by | Fillmore <fillmore_remove@hotmail.com> |
|---|---|
| First post | 2016-03-07 17:51 -0500 |
| Last post | 2016-03-08 21:10 -0500 |
| Articles | 13 — 9 participants |
Back to article view | Back to comp.lang.python
Pythonic love Fillmore <fillmore_remove@hotmail.com> - 2016-03-07 17:51 -0500
Re: Pythonic love Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-03-07 22:52 +0000
Re: Pythonic love sohcahtoa82@gmail.com - 2016-03-07 15:03 -0800
Re: Pythonic love Fillmore <fillmore_remove@hotmail.com> - 2016-03-07 18:12 -0500
Re: Pythonic love Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-07 16:13 -0700
Re: Pythonic love Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-07 23:19 +0000
Re: Pythonic love jmp <jeanmichel@sequans.com> - 2016-03-08 14:25 +0100
Re: Pythonic love justin walters <walters.justin01@gmail.com> - 2016-03-08 08:49 -0800
Re: Pythonic love Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-08 16:55 +0000
Re: Pythonic love jmp <jeanmichel@sequans.com> - 2016-03-08 18:00 +0100
Re: Pythonic love justin walters <walters.justin01@gmail.com> - 2016-03-08 09:17 -0800
Re: Pythonic love jstitch@invernalia.homelinux.net (Javier Novoa C.) - 2016-03-08 11:27 -0600
Re: Pythonic love Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-03-08 21:10 -0500
| From | Fillmore <fillmore_remove@hotmail.com> |
|---|---|
| Date | 2016-03-07 17:51 -0500 |
| Subject | Pythonic love |
| Message-ID | <nbl0lo$5o5$1@gioia.aioe.org> |
learning Python from Perl here. Want to do things as Pythonicly as possible.
I am reading a TSV, but need to skip the first 5 lines. The following
works, but wonder if there's a more pythonc way to do things. Thanks
ctr = 0
with open(prfile,mode="rt",encoding='utf-8') as pfile:
for line in pfile:
ctr += 1
if ctr < 5:
continue
allVals = line.strip().split("\t")
print(allVals)
[toc] | [next] | [standalone]
| From | Rob Gaddi <rgaddi@highlandtechnology.invalid> |
|---|---|
| Date | 2016-03-07 22:52 +0000 |
| Message-ID | <nbl0n1$n5d$1@dont-email.me> |
| In reply to | #104262 |
Fillmore wrote:
>
> learning Python from Perl here. Want to do things as Pythonicly as possible.
>
> I am reading a TSV, but need to skip the first 5 lines. The following
> works, but wonder if there's a more pythonc way to do things. Thanks
>
> ctr = 0
> with open(prfile,mode="rt",encoding='utf-8') as pfile:
> for line in pfile:
> ctr += 1
>
> if ctr < 5:
> continue
>
> allVals = line.strip().split("\t")
> print(allVals)
for lineno, line in enumerate(pfile, start=1):
will save you maintaining your own counter. But other than that, nah,
that's pretty much the approach you're looking for.
--
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order. See above to fix.
[toc] | [prev] | [next] | [standalone]
| From | sohcahtoa82@gmail.com |
|---|---|
| Date | 2016-03-07 15:03 -0800 |
| Message-ID | <ecc1c55f-4d49-4eb0-9f53-4679911e2434@googlegroups.com> |
| In reply to | #104262 |
On Monday, March 7, 2016 at 2:51:50 PM UTC-8, Fillmore wrote:
> learning Python from Perl here. Want to do things as Pythonicly as possible.
>
> I am reading a TSV, but need to skip the first 5 lines. The following
> works, but wonder if there's a more pythonc way to do things. Thanks
>
> ctr = 0
> with open(prfile,mode="rt",encoding='utf-8') as pfile:
> for line in pfile:
> ctr += 1
>
> if ctr < 5:
> continue
>
> allVals = line.strip().split("\t")
> print(allVals)
I'd read all the lines at once and then just slice the list.
with open(prfile, mode="rt", encoding="utf-8") as pfile:
lines = pfile.readlines()[5:]
for line in lines:
allVals = line.strip().split("\t")
print(allVals)
Obviously, this will only work well if your file is a reasonable size, as it will store the entire thing in memory at once.
On a side note, your "with open..." line uses inconsistent quoting. You have "" on one string, but '' on another.
[toc] | [prev] | [next] | [standalone]
| From | Fillmore <fillmore_remove@hotmail.com> |
|---|---|
| Date | 2016-03-07 18:12 -0500 |
| Message-ID | <nbl1sf$1m7j$1@gioia.aioe.org> |
| In reply to | #104264 |
On 3/7/2016 6:03 PM, sohcahtoa82@gmail.com wrote: > On a side note, your "with open..." line uses inconsistent quoting. > You have "" on one string, but '' on another. Thanks. I'll make sure I flog myself three times later tonight...
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2016-03-07 16:13 -0700 |
| Message-ID | <mailman.0.1457392456.15725.python-list@python.org> |
| In reply to | #104262 |
On Mon, Mar 7, 2016 at 3:51 PM, Fillmore <fillmore_remove@hotmail.com> wrote:
>
> learning Python from Perl here. Want to do things as Pythonicly as possible.
>
> I am reading a TSV, but need to skip the first 5 lines. The following works,
> but wonder if there's a more pythonc way to do things. Thanks
I'd probably use itertools.islice.
from itertools import islice
for line in isiice(pfile, 5, None):
allVals = line.strip().split("\t")
print(allVals)
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2016-03-07 23:19 +0000 |
| Message-ID | <mailman.2.1457392797.15725.python-list@python.org> |
| In reply to | #104262 |
On 07/03/2016 22:51, Fillmore wrote:
>
> learning Python from Perl here. Want to do things as Pythonicly as
> possible.
>
> I am reading a TSV, but need to skip the first 5 lines. The following
> works, but wonder if there's a more pythonc way to do things. Thanks
>
> ctr = 0
> with open(prfile,mode="rt",encoding='utf-8') as pfile:
> for line in pfile:
> ctr += 1
>
> if ctr < 5:
> continue
>
> allVals = line.strip().split("\t")
> print(allVals)
Something like this, completely untested.
with open(prfile,mode="rt",encoding='utf-8') as pfile:
lineIter = iter(pfile)
for _ in range(5):
next(lineIter)
for line in lineIter:
allVals = line.strip().split("\t")
print(allVals)
I'd also recommend that you use the csv module
https://docs.python.org/3/library/csv.html which can also cope with tsv
files, it's far more reliable than relying on a simple call to split().
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | jmp <jeanmichel@sequans.com> |
|---|---|
| Date | 2016-03-08 14:25 +0100 |
| Message-ID | <mailman.42.1457443519.15725.python-list@python.org> |
| In reply to | #104262 |
On 03/07/2016 11:51 PM, Fillmore wrote:
>
> learning Python from Perl here. Want to do things as Pythonicly as
> possible.
>
> I am reading a TSV, but need to skip the first 5 lines. The following
> works, but wonder if there's a more pythonc way to do things. Thanks
>
> ctr = 0
> with open(prfile,mode="rt",encoding='utf-8') as pfile:
> for line in pfile:
> ctr += 1
>
> if ctr < 5:
> continue
>
> allVals = line.strip().split("\t")
> print(allVals)
what about a generator expression ? The (not so)new hype:
with open(prfile,mode="rt",encoding='utf-8') as pfile:
for values in (l.strip().split("\t") for (i, l) in enumerate(pfile)
if i >=5):
print values
slightly dense, could be better with a lambda function
tovalues = lambda l: l.strip().split("\t")
with open(prfile,mode="rt",encoding='utf-8') as pfile:
for values in (tovalues(l) for (i, l) in enumerate(pfile) if i >=5):
print values
This should even work quite efficiently on big files, because I don't
thing no more than one line is in memory at a given time.
jm
[toc] | [prev] | [next] | [standalone]
| From | justin walters <walters.justin01@gmail.com> |
|---|---|
| Date | 2016-03-08 08:49 -0800 |
| Message-ID | <mailman.48.1457455785.15725.python-list@python.org> |
| In reply to | #104262 |
Correct me if I'm wrong, but don't python generators usually use the yield
statement so they can be used in list comprehensions?
On Mar 8, 2016 5:27 AM, "jmp" <jeanmichel@sequans.com> wrote:
> On 03/07/2016 11:51 PM, Fillmore wrote:
>
>>
>> learning Python from Perl here. Want to do things as Pythonicly as
>> possible.
>>
>> I am reading a TSV, but need to skip the first 5 lines. The following
>> works, but wonder if there's a more pythonc way to do things. Thanks
>>
>> ctr = 0
>> with open(prfile,mode="rt",encoding='utf-8') as pfile:
>> for line in pfile:
>> ctr += 1
>>
>> if ctr < 5:
>> continue
>>
>> allVals = line.strip().split("\t")
>> print(allVals)
>>
>
> what about a generator expression ? The (not so)new hype:
>
> with open(prfile,mode="rt",encoding='utf-8') as pfile:
> for values in (l.strip().split("\t") for (i, l) in enumerate(pfile) if i
> >=5):
> print values
>
> slightly dense, could be better with a lambda function
>
> tovalues = lambda l: l.strip().split("\t")
> with open(prfile,mode="rt",encoding='utf-8') as pfile:
> for values in (tovalues(l) for (i, l) in enumerate(pfile) if i >=5):
> print values
>
>
> This should even work quite efficiently on big files, because I don't
> thing no more than one line is in memory at a given time.
>
> jm
>
>
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2016-03-08 16:55 +0000 |
| Message-ID | <mailman.49.1457456193.15725.python-list@python.org> |
| In reply to | #104262 |
On 08/03/2016 16:49, justin walters wrote: > Correct me if I'm wrong, but don't python generators usually use the yield > statement so they can be used in list comprehensions? Please don't top post on this list, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | jmp <jeanmichel@sequans.com> |
|---|---|
| Date | 2016-03-08 18:00 +0100 |
| Message-ID | <mailman.50.1457456451.15725.python-list@python.org> |
| In reply to | #104262 |
On 03/08/2016 05:49 PM, justin walters wrote: > Correct me if I'm wrong, but don't python generators usually use the yield > statement so they can be used in list comprehensions? Hello, Please don't top post. Generator expressions are different from generator functions. They are both generators but use a different syntax. generator function: def func(iterable): for i in iterable: yield i the generator expression version would be (i for i in iterable) Both have their use cases but everytime you can actually use an generator expression, it's probably the correct thing to do. Cheers, jm
[toc] | [prev] | [next] | [standalone]
| From | justin walters <walters.justin01@gmail.com> |
|---|---|
| Date | 2016-03-08 09:17 -0800 |
| Message-ID | <mailman.51.1457457429.15725.python-list@python.org> |
| In reply to | #104262 |
Sorry about the top posting. I'm new to mailing lists. I should just reply to the python-list@python.org address then? Also, thank you for the generator clarification. On Mar 8, 2016 9:09 AM, "jmp" <jeanmichel@sequans.com> wrote: > On 03/08/2016 05:49 PM, justin walters wrote: > >> Correct me if I'm wrong, but don't python generators usually use the yield >> statement so they can be used in list comprehensions? >> > > Hello, > > Please don't top post. > > Generator expressions are different from generator functions. They are > both generators but use a different syntax. > > generator function: > > def func(iterable): > for i in iterable: yield i > > > the generator expression version would be > > (i for i in iterable) > > > Both have their use cases but everytime you can actually use an generator > expression, it's probably the correct thing to do. > > Cheers, > > jm > > -- > https://mail.python.org/mailman/listinfo/python-list >
[toc] | [prev] | [next] | [standalone]
| From | jstitch@invernalia.homelinux.net (Javier Novoa C.) |
|---|---|
| Date | 2016-03-08 11:27 -0600 |
| Message-ID | <87r3fkanid.fsf@invernalia.homelinux.net> |
| In reply to | #104348 |
justin walters <walters.justin01@gmail.com> writes: > Sorry about the top posting. I'm new to mailing lists. I should just reply > to the python-list@python.org address then? > > Also, thank you for the generator clarification. > On Mar 8, 2016 9:09 AM, "jmp" <jeanmichel@sequans.com> wrote: ^ that's top posting.... always reply inline or at the bottom
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2016-03-08 21:10 -0500 |
| Message-ID | <mailman.67.1457489397.15725.python-list@python.org> |
| In reply to | #104351 |
On Tue, 08 Mar 2016 11:27:22 -0600, jstitch@invernalia.homelinux.net
(Javier Novoa C.) declaimed the following:
>justin walters <walters.justin01@gmail.com> writes:
>
>> Sorry about the top posting. I'm new to mailing lists. I should just reply
>> to the python-list@python.org address then?
>>
>> Also, thank you for the generator clarification.
>> On Mar 8, 2016 9:09 AM, "jmp" <jeanmichel@sequans.com> wrote:
>
>^ that's top posting.... always reply inline or at the bottom
And, preferably, also trim anything that wasn't relevant (but this is
so short any trimming would lose context <G>)
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web