Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8545 > unrolled thread
| Started by | Amaninder Singh <asingh0412@gmail.com> |
|---|---|
| First post | 2011-06-28 12:17 -0700 |
| Last post | 2011-06-29 09:31 +0200 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
Change the name with the random names in a text file Amaninder Singh <asingh0412@gmail.com> - 2011-06-28 12:17 -0700
Re: Change the name with the random names in a text file Chris Rebert <clp2@rebertia.com> - 2011-06-28 23:16 -0700
Re: Change the name with the random names in a text file Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-06-29 09:31 +0200
| From | Amaninder Singh <asingh0412@gmail.com> |
|---|---|
| Date | 2011-06-28 12:17 -0700 |
| Subject | Change the name with the random names in a text file |
| Message-ID | <190e6c5d-00d2-4043-82f2-a33bf9be5fac@h25g2000prf.googlegroups.com> |
Hi Guys, I am fairly new to the language and programing. I am trying to solve a problem in a text file. Where names are something like in this manner [**Name2 (NI) 98**] [**Last Name (STitle) 97**] [**First Name4 (NamePattern1) 93**] [**Last Name (NamePattern1) 94**] ([**Name (NI) 95**]) [**Last Name (un) 96**] I am trying to change these with random names. Any ideas or how should I do it. Any help appreciated. Thank in advance
[toc] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-06-28 23:16 -0700 |
| Message-ID | <mailman.493.1309328210.1164.python-list@python.org> |
| In reply to | #8545 |
On Tue, Jun 28, 2011 at 12:17 PM, Amaninder Singh <asingh0412@gmail.com> wrote: > Hi Guys, > I am fairly new to the language and programing. I am trying to solve a > problem in a text file. Where names are something like in this manner > [**Name2 (NI) 98**] > > [**Last Name (STitle) 97**] > [**First Name4 (NamePattern1) 93**] > [**Last Name (NamePattern1) 94**] > ([**Name (NI) 95**]) > [**Last Name (un) 96**] > I am trying to change these with random names. The problem (particularly the exact meaning of your input file format) isn't quite clear. Sample output would be useful to those trying to help you. Cheers, Chris
[toc] | [prev] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2011-06-29 09:31 +0200 |
| Message-ID | <pn0rd8-eh7.ln1@satorlaser.homedns.org> |
| In reply to | #8545 |
Amaninder Singh wrote:
> I am fairly new to the language and programing. I am trying to solve a
> problem in a text file. Where names are something like in this manner
> [**Name2 (NI) 98**]
>
> [**Last Name (STitle) 97**]
> [**First Name4 (NamePattern1) 93**]
> [**Last Name (NamePattern1) 94**]
> ([**Name (NI) 95**])
> [**Last Name (un) 96**]
> I am trying to change these with random names.
First thing is to read the text file. It looks to me that these are records
that consist of multiple lines. Depending on the exact layout you could read
and store them in a plain list or maybe a dictionary. As part of this sub-
task, output each record, too, so you can manually verify that its fields
are correct.
with open(filename) as f:
lines = list(f)
print lines
This should get you started, it will read the file into a list for further
handling. You might need "codecs.open" instead if you have special
requirements concerning the encoding.
Then, in a second step, you just go through your list of records and modify
them however you want. If there are restrictions to the overall form of a
record or its contents, be sure to validate the record both after reading
and after modification.
In a third step, you just write the records to an output file, but by then
this will be an easy task for you. Of course, reading back the output and
validating it is a good idea. Actually, you can also skip the second step
for now and just write back the data you previously read without
modification.
If you have any problems with these, be more specific with what you want,
what you have already and where your problems are. In particular, don't ask
for code, as that looks as if you were trying to get someone else to do your
homework for you without making an effort yourself.
Good luck!
Uli
--
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web