473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

String Replacement

Hello everyone, I've got a simple one today. I have a string and I want to remove all carriage returns ('\n') between the characters [ACGU] and [ACGU] and preserve the other ones. For example:

'Musmusculuslet-7gstem-loop\nCCAGGCUGAGGUAGUAGUUUGUACAGUUUGAGGGUCUAUGAUAC CACCCGGUACAGGAGA\nUAACUGUACAGGCCACUGCCUUGCCAGG\n
Musmusculuslet-7istem-loop\nCUGGCUGAGGUAGUAGUUUGUGCUGUUGGUCGGGUUGUGACAUU GCCCGCUGUGGAGAUA\nACUGCGCAAGCUACUGCCUUGCUAG\n'

editted string:

'Musmusculuslet-7gstem-loop\nCCAGGCUGAGGUAGUAGUUUGUACAGUUUGAGGGUCUAUGAUAC CACCCGGUACAGGAGAUAACUGUACAGGCCACUGCCUUGCCAGG\n
Musmusculuslet-7istem-loop\nCUGGCUGAGGUAGUAGUUUGUGCUGUUGGUCGGGUUGUGACAUU GCCCGCUGUGGAGAUAACUGCGCAAGCUACUGCCUUGCUAG\n'

I tried the following code but I got a syntax error:

Expand|Select|Wrap|Line Numbers
  1. for a in chunk3:
  2.     if ([ACGU]'\n'[ACGU]):
  3.                chunk3 = chunk3.replace('\n','')
  4.  
Thanks,

Mark
Aug 8 '07 #1
5 2847
ilikepython
844 Expert 512MB
Hello everyone, I've got a simple one today. I have a string and I want to remove all carriage returns ('\n') between the characters [ACGU] and [ACGU] and preserve the other ones. For example:

'Musmusculuslet-7gstem-loop\nCCAGGCUGAGGUAGUAGUUUGUACAGUUUGAGGGUCUAUGAUAC CACCCGGUACAGGAGA\nUAACUGUACAGGCCACUGCCUUGCCAGG\n
Musmusculuslet-7istem-loop\nCUGGCUGAGGUAGUAGUUUGUGCUGUUGGUCGGGUUGUGACAUU GCCCGCUGUGGAGAUA\nACUGCGCAAGCUACUGCCUUGCUAG\n'

editted string:

'Musmusculuslet-7gstem-loop\nCCAGGCUGAGGUAGUAGUUUGUACAGUUUGAGGGUCUAUGAUAC CACCCGGUACAGGAGAUAACUGUACAGGCCACUGCCUUGCCAGG\n
Musmusculuslet-7istem-loop\nCUGGCUGAGGUAGUAGUUUGUGCUGUUGGUCGGGUUGUGACAUU GCCCGCUGUGGAGAUAACUGCGCAAGCUACUGCCUUGCUAG\n'

I tried the following code but I got a syntax error:

Expand|Select|Wrap|Line Numbers
  1. for a in chunk3:
  2.     if ([ACGU]'\n'[ACGU]):
  3.                chunk3 = chunk3.replace('\n','')
  4.  
Thanks,

Mark
Try this:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. s = "Musmusculuslet-7gstem-loop\nCCAGGCUGAGGUAGUAGUUUGUACAGUUUGAGGGUCUAUGAUACCACCCGGUACAGGAGA\nUAACUGUACAGGCCACUGCCUUGCCAGG\n"
  4.  
  5. patt = re.compile(r"[ACGU]\n[ACGU]")
  6. matches = patt.findall(s)
  7.  
  8. for m in matches:
  9.     s = s.replace(m, m[0] + m[2])
  10.  
Aug 8 '07 #2
bvdet
2,851 Expert Mod 2GB
Hello everyone, I've got a simple one today. I have a string and I want to remove all carriage returns ('\n') between the characters [ACGU] and [ACGU] and preserve the other ones. For example:

'Musmusculuslet-7gstem-loop\nCCAGGCUGAGGUAGUAGUUUGUACAGUUUGAGGGUCUAUGAUAC CACCCGGUACAGGAGA\nUAACUGUACAGGCCACUGCCUUGCCAGG\n
Musmusculuslet-7istem-loop\nCUGGCUGAGGUAGUAGUUUGUGCUGUUGGUCGGGUUGUGACAUU GCCCGCUGUGGAGAUA\nACUGCGCAAGCUACUGCCUUGCUAG\n'

editted string:

'Musmusculuslet-7gstem-loop\nCCAGGCUGAGGUAGUAGUUUGUACAGUUUGAGGGUCUAUGAUAC CACCCGGUACAGGAGAUAACUGUACAGGCCACUGCCUUGCCAGG\n
Musmusculuslet-7istem-loop\nCUGGCUGAGGUAGUAGUUUGUGCUGUUGGUCGGGUUGUGACAUU GCCCGCUGUGGAGAUAACUGCGCAAGCUACUGCCUUGCUAG\n'

I tried the following code but I got a syntax error:

Expand|Select|Wrap|Line Numbers
  1. for a in chunk3:
  2.     if ([ACGU]'\n'[ACGU]):
  3.                chunk3 = chunk3.replace('\n','')
  4.  
Thanks,

Mark
It looks like you are trying to implement a regex solution without understanding how it works. This seems to do what you want:
Expand|Select|Wrap|Line Numbers
  1. import re
  2. patt = re.compile(r'[ACGU]\n[ACGU]')
  3. s = 'Musmusculuslet-7gstem-loop\nCCAGGCUGAGGUAGUAGUUUGUACAGUUUGAGGGUCUAUGAUACCACCCGGUACAGGAGA\nUAACUGUACAGGCCACUGCCUUGCCAGG\nMusmusculuslet-7istem-loop\nCUGGCUGAGGUAGUAGUUUGUGCUGUUGGUCGGGUUGUGACAUUGCCCGCUGUGGAGAUA\nACUGCGCAAGCUACUGCCUUGCUAG\n'
  4. s1 = s
  5. for item in patt.findall(s):
  6.     s1 = s1.replace(item, (item.replace('\n', '')))
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> Musmusculuslet-7gstem-loop
  2. CCAGGCUGAGGUAGUAGUUUGUACAGUUUGAGGGUCUAUGAUACCACCCGGUACAGGAGAUAACUGUACAGGCCACUGCCUUGCCAGG
  3. Musmusculuslet-7istem-loop
  4. CUGGCUGAGGUAGUAGUUUGUGCUGUUGGUCGGGUUGUGACAUUGCCCGCUGUGGAGAUAACUGCGCAAGCUACUGCCUUGCUAG
  5.  
  6. >>> 
Step by step:
1. Import the 're' module
2. Define and compile the pattern to match substrings in your string: a character in the set 'ACGU' followed by '\n' followed by a character in the set 'ACGU'.
3. Use the compiled pattern object to find all occurrences of the matched patterns.
>>> patt.findall(s)
['A\nU', 'A\nA']
>>>
4. Use string method replace to replace each '\n' with "".
HTH
Aug 8 '07 #3
bvdet
2,851 Expert Mod 2GB
You beat me to it ilikepython! :(
Aug 8 '07 #4
Thanks for the guidance. My question is why are you creating a second string (s1), and running the pattern and replacing in that string? I'm still working my way around understanding regex's.

Mark
Aug 9 '07 #5
bvdet
2,851 Expert Mod 2GB
Thanks for the guidance. My question is why are you creating a second string (s1), and running the pattern and replacing in that string? I'm still working my way around understanding regex's.

Mark
No reason other that to do further manipulations at the interactive prompt.
Aug 9 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Juha Suni | last post by:
Hi! I have managed to live without using too much regular expressions so far, and now that I need one, I need some help too. I have a string containing a (possibly large) block of html. I need...
5
by: Roose | last post by:
How can I do a "".replace operation which tells me if anything was actually replaced? Right now I am doing something like: if searchTerm in text: text = text.replace( searchTerm, 'other' ) ...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
13
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
35
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a...
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
5
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h>...
4
by: Phil Sandler | last post by:
Hello, What is the fastest/most efficient way of doing string replacement in csharp/.net? An example would be: "Hello, my name is {FirstName}, and I live in the town of {City} with my wife...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.