removing characters before writing to file

E

eight02645999

hi
i have some output that returns a lines of tuples eg

('sometext1', 1421248118, 1, 'P ')
('sometext2', 1421248338, 2, 'S ')
and so on
.....

I tried this
re.sub(r" '() ",'',str(output)) but it only get rid of the ' and not
the braces. I need to write the output to a file such that

sometext1, 1421248118, 1, P
sometext2, 1421248338, 2, S

I also tried escaping , re.sub(r" '\(\) ",'',str(output)) but also did
not work
How can i get rid of the braces before writing to file? thanks
 
J

John Zenger

As mentioned in the thread, it makes sense to build the desired output
you want from the tuple, rather than converting the tuple to a string
and then doing replace operations on the string.

If you do want to go the replace route, you don't need the power of
regex substitutions for what you are interested in. Just try the
replace method:
'sometext1, 1421248118, 1, P '

or, more elegantly:
>>> "".join([x for x in foo if x not in ['(',')','\''] ])
'sometext1, 1421248118, 1, P '

However, all of these replace-based solutions are bad because they will
not only replace the apostrophes and parentheses between the strings,
but also within them; what if "sometext1" is actually "John's Text?"
You are much better off building your desired output from the actual
tuple data yourself.
 
P

Peter Otten

i have some output that returns a lines of tuples eg

('sometext1', 1421248118, 1, 'P ')
('sometext2', 1421248338, 2, 'S ')
and so on
....

I tried this
re.sub(r" '() ",'',str(output)) but it only get rid of the ' and not
the braces. I need to write the output to a file such that

sometext1, 1421248118, 1, P
sometext2, 1421248338, 2, S

I also tried escaping , re.sub(r" '\(\) ",'',str(output)) but also did
not work
How can i get rid of the braces before writing to file? thanks

I'd use a csv.writer:
.... ('sometext1', 1421248118, 1, 'P '),
.... ('sometext2', 1421248338, 2, 'S ')
.... ]sometext1,1421248118,1,P
sometext2,1421248338,2,S

Peter
 
B

bonono

hi
i have some output that returns a lines of tuples eg

('sometext1', 1421248118, 1, 'P ')
('sometext2', 1421248338, 2, 'S ')
and so on
....

I tried this
re.sub(r" '() ",'',str(output)) but it only get rid of the ' and not
the braces. I need to write the output to a file such that

sometext1, 1421248118, 1, P
sometext2, 1421248338, 2, S

I also tried escaping , re.sub(r" '\(\) ",'',str(output)) but also did
not work
How can i get rid of the braces before writing to file? thanks

If it is as uniform as shown, may be you can strip them.

"('something', 1234, 1, 'P ')".strip(" ()")
 
S

Stefan Neumann

hi
i have some output that returns a lines of tuples eg

('sometext1', 1421248118, 1, 'P ')
('sometext2', 1421248338, 2, 'S ')
and so on
....

If the braces are always at the begining and at the end of the string,
you could also use:

>>> "('sometext1', 1421248118, 1, 'P ')"[1:-1]
"'sometext1', 1421248118, 1, 'P '"

cheers
Stefan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBD61es/gltz1ptagYRAvIHAJsFnTkmUZLszgLLT0krm1A6L70BhACgjS2w
cPvme2VEFegA0rX00ulh/kE=
=yeij
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top