473,804 Members | 3,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

removing characters before writing to file

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

Feb 9 '06 #1
5 1620
', '.join(map(str, output))

Feb 9 '06 #2
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:
foo = "('sometext 1', 1421248118, 1, 'P ')"
foo.replace("\' ", "").replace("(" , "").replace(")" , "") '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.

ei***********@y ahoo.com wrote: 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

Feb 9 '06 #3
ei***********@y ahoo.com wrote:
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:
import csv, sys
data = [ .... ('sometext1', 1421248118, 1, 'P '),
.... ('sometext2', 1421248338, 2, 'S ')
.... ] w = csv.writer(sys. stdout)
w.writerows(dat a)

sometext1,14212 48118,1,P
sometext2,14212 48338,2,S

Peter

Feb 9 '06 #4

ei***********@y ahoo.com wrote:
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.

"('somethin g', 1234, 1, 'P ')".strip(" ()")

Feb 9 '06 #5
ei***********@y ahoo.com wrote:
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:
"('sometext 1', 1421248118, 1, 'P ')"[1:-1]

"'sometext1 ', 1421248118, 1, 'P '"

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

iD8DBQBD61es/gltz1ptagYRAvIH AJsFnTkmUZLszgL LT0krm1A6L70BhA CgjS2w
cPvme2VEFegA0rX 00ulh/kE=
=yeij
-----END PGP SIGNATURE-----

Feb 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8038
by: Tom | last post by:
Hi all, I've searched high and low (google, the FAQ & so on) and I can't seem to find any way of doing this. What I am trying to do is to edit text files (eg. ini files) and remove/replace data in the file without having to create new files and transferring the data and so on. So one function I was trying to build was a "removechars" function which would simply remove x chars from the file from the current position. The code may not be...
2
2289
by: Keith | last post by:
A2003, XP Pro. I'm writing a VBA procedure to remove wildcard characters from a number of fields in various tables. I'm calling a sub routine and passing in strings representing the character to be replaced, the field name and the table name. All works fine until I try to get the data string out of the field by referencing the string variable, at which point I get an "object variable not set" error. I know this is going to be...
3
2211
by: Frank Niessink | last post by:
Hi list, First of all, I wish you all a happy 2006. I have a small question that googling didn't turn up an answer for. So hopefully you'll be kind enough to send me in the right direction. I'm developing a desktop application, called Task Coach, that saves its domain objects (tasks, mostly :-) in an XML file. Users have reported that sometimes their Task Coach file would become unreadable by Task Coach after copying information from...
16
3434
by: Hugh Janus | last post by:
Hi all, I posted a couple of weeks ago with what I thought was a problem with the file system reading accented characters however, after debugging line by line I have now found the true problem. I am storing a list of files in an XML file as a sort of database. Some of these filenames have accented characters (i.e. á é í ó ú or ñ). However, upon writing the filename to the XML file, the accented character is dropped. This causes a...
2
6722
by: joakim.hove | last post by:
Hello, I am having great problems writing norwegian characters æøå to file from a python application. My (simplified) scenario is as follows: 1. I have a web form where the user can enter his name. 2. I use the cgi module module to get to the input from the user: .... name = form.value
7
4924
by: Trac Bannon | last post by:
When I load XML from a file into a dotNet XMLDataDocument, the UTF-8 codes are resolved but the 5 special XML entities are not. How can I force those 5 special character types to be translated?
14
5783
by: Zoro | last post by:
My task is to read html files from disk and save them onto SQL Server database field. I have created an nvarchar(max) field to hold them. The problem is that some characters, particularly html entities, and French/German special characters are lost and/or replaced by a question mark. This is really frustrating. I have tried using StreamReader with ALL the encodings available and none work correctly. Each encoding handles some characters...
0
5148
by: Benny the Guard | last post by:
I have a string value that may contain some unprintable characters. I am using the xml.sax.escape to remove the the <, > and & characters fine but it seems to leave in the \n characters. Later this string gets added to some XML, which is UTF-8 encoded, which fails since charcater code 8 is invalid UTF-8. I figured it was a problem with ASCII -> UTF-8 (by the way how do you do newlines in UTF-8 encoded XML?). So I tried the following...
13
17635
by: Eps | last post by:
Hi there, I believe all strings in .net are unicode by default, I am looking for a way to remove all non ascii characters from a string (or optionally replace them). There is an article on code project which kind of looks like it does what I want but I can't help thinking it makes it more complex than it needs to be.
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.