Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80216
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python.list@tim.thechases.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.006 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'assign': 0.07; 'salary,': 0.07; '101': 0.09; 'iterate': 0.09; 'variable,': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; '"build': 0.16; '-tkc': 0.16; '.txt': 0.16; 'csv': 0.16; 'file"': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'example': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'this?': 0.23; 'file.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'file:': 0.31; 'file': 0.32; 'class': 0.32; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'something': 0.35; 'objects': 0.35; 'yield': 0.36; 'useful': 0.36; 'employee': 0.37; 'so,': 0.37; 'received:10': 0.37; 'files': 0.38; 'skip:p 20': 0.39; 'read': 0.60; 'new': 0.61; 'name': 0.63; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'received:46': 0.66; 'age': 0.80; 'complex,': 0.84; 'subject:via': 0.84 |
| X-Sender-Id | wwwh|x-authuser|tim@thechases.com |
| X-Sender-Id | wwwh|x-authuser|tim@thechases.com |
| X-MC-Relay | Neutral |
| X-MailChannels-SenderId | wwwh|x-authuser|tim@thechases.com |
| X-MailChannels-Auth-Id | wwwh |
| X-MC-Loop-Signature | 1415113371646:4126933217 |
| X-MC-Ingress-Time | 1415113371645 |
| Date | Tue, 4 Nov 2014 09:05:30 -0600 |
| From | Tim Chase <python.list@tim.thechases.com> |
| To | Fatih Güven <mfthguven@gmail.com> |
| Subject | Re: generating unique variable name via loops |
| In-Reply-To | <ad63b305-5ffd-41b3-bb58-080386a90154@googlegroups.com> |
| References | <8d3d202f-3bb8-4683-8dd7-b065fab06c34@googlegroups.com> <8df0b347-0569-4505-95f8-12baf44a1743@googlegroups.com> <m3ajo4$kit$1@dont-email.me> <mailman.15445.1415108267.18130.python-list@python.org> <ad63b305-5ffd-41b3-bb58-080386a90154@googlegroups.com> |
| X-Mailer | Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| X-AuthUser | tim@thechases.com |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15462.1415125749.18130.python-list@python.org> (permalink) |
| Lines | 56 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1415125749 news.xs4all.nl 2836 [2001:888:2000:d::a6]:59517 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:80216 |
Show key headers only | View raw
On 2014-11-04 05:53, Fatih Güven wrote:
> > > for x in range(1,10):
> > > exec("list%d = []" % x)
> >
> > Why would you do this?
>
> I have a structured and repetitive data. I want to read a .txt file
> line by line and classified it to call easily. For example
> employee1 has a name, a salary, shift, age etc. and employee2 and
> other 101 employee have all of it.
>
> Call employee1.name or employee2.salary and assign it to a new
> variable, something etc. --
This sounds remarkably like a CSV or tab-delimited file. If so, the
way to do it would be
import csv
with open("data.txt", "rb") as f:
dr = csv.DictReader(f)
for row in dr:
do_something(row["Name"], row["salary"])
If the file format is more complex, it's often useful to create a
generator to simplify the logic:
class Person:
def __init__(self,
name="",
salary=0,
shift="",
):
self.name = name
self.salary = salary
self.shift = shift
def various_person_methods(self, ...):
pass
def people_from_file(f):
"build Person objects as you iterate over the file"
for row in file:
person = Person( ... )
yield person
with open("data.txt", "r"):
for person in people_from_file(f):
do_something(person)
You can then reuse that generator with multiple files if you need.
-tkc
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
generating unique variable name via loops Fatih Güven <mfthguven@gmail.com> - 2014-11-04 03:29 -0800
Re: generating unique variable name via loops Fatih Güven <mfthguven@gmail.com> - 2014-11-04 04:48 -0800
Re: generating unique variable name via loops Veek M <vek.m1234@gmail.com> - 2014-11-04 18:49 +0630
Re: generating unique variable name via loops Peter Otten <__peter__@web.de> - 2014-11-04 14:37 +0100
Re: generating unique variable name via loops Fatih Güven <mfthguven@gmail.com> - 2014-11-04 05:53 -0800
Re: generating unique variable name via loops Peter Otten <__peter__@web.de> - 2014-11-04 16:00 +0100
Re: generating unique variable name via loops Fatih Güven <mfthguven@gmail.com> - 2014-11-04 07:36 -0800
Re: generating unique variable name via loops Peter Otten <__peter__@web.de> - 2014-11-04 17:06 +0100
Re: generating unique variable name via loops Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-04 17:15 +0000
Re: generating unique variable name via loops Tim Chase <python.list@tim.thechases.com> - 2014-11-04 09:05 -0600
Re: generating unique variable name via loops Fatih Güven <mfthguven@gmail.com> - 2014-11-04 05:45 -0800
Re: generating unique variable name via loops Veek M <vek.m1234@gmail.com> - 2014-11-04 19:20 +0630
Re: generating unique variable name via loops Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-04 17:14 +0000
Re: generating unique variable name via loops Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-04 09:00 -0500
Re: generating unique variable name via loops Peter Otten <__peter__@web.de> - 2014-11-04 14:09 +0100
Re: generating unique variable name via loops Matthew Ruffalo <mmr15@case.edu> - 2014-11-04 10:04 -0500
csiph-web