Python Script To Split Dna Strings Into New Lines
4
1
Entering edit mode
14.1 years ago
Studentguy ▴ 70

I have this string:

'ACC': 2, 'ACACAGTTTGCGCGCGCGGGG': 1, 'ACCGTA': 3, 'GCTTTAAAAAGGCAA': 1, 'ACCGTAACCTTG': 2

I want it to print out like this:

'ACC': 2,

'ACACAGTTTGCGCGCGCGGGG': 1,

'ACCGTA': 3,

'GCTTTAAAAAGGCAA': 1,

'ACCGTAACCTTG': 2

I know im supposed to use split() or splitlines() to get it to break after every comma but I am dumbfounded right now and it seems like i am missing something simple

python sequence format • 5.3k views
ADD COMMENT
0
Entering edit mode

Note that by accident it seems that you included the same output in for both input and output, please edit your question and change that.

ADD REPLY
4
Entering edit mode
14.1 years ago

In case your input is a string, you just have to use the string's .split method to split it into a list:

>>> original_string = "'ACC': 2, 'ACACAGTTTGCGCGCGCGGGG': 1, 'ACCGTA': 3, 'GCTTTAAAAAGGCAA': 1, 'ACCGTAACCTTG': 2"
>>> splitted = original_string.split(', ')
>>> splitted
["'ACC': 2",
 "'ACACAGTTTGCGCGCGCGGGG': 1",
 "'ACCGTA': 3",
 "'GCTTTAAAAAGGCAA': 1",
 "'ACCGTAACCTTG': 2"]
>>> for el in splitted: 
        print el + ','
'ACC': 2,
'ACACAGTTTGCGCGCGCGGGG': 1,
'ACCGTA': 3,
'GCTTTAAAAAGGCAA': 1,
'ACCGTAACCTTG': 2,

However, I suspect that you just want to print the contents of a dictionary in order to see them more clearly. If that is the case, have a look at the pprint function:

>>> import pprint
>>> mydict = {'ACC': 2, 'ACACAGTTTGCGCGCGCGGGG': 1, 'ACCGTA': 3, 'GCTTTAAAAAGGCAA': 1, 'ACCGTAACCTTG': 2}
>>> pprint.pprint(mydict)            
{'ACACAGTTTGCGCGCGCGGGG': 1,
 'ACC': 2,
 'ACCGTA': 3,
 'ACCGTAACCTTG': 2,
 'GCTTTAAAAAGGCAA': 1}

Also, consider using ipython instead of the standard python interpreter, which has many features and pretty-prints dictionaries by default.

ADD COMMENT
3
Entering edit mode
14.1 years ago

Hi,

In the case where you really have a string to work with (like in your question), try this:

my_string = "'ACC': 2, 'ACACAGTTTGCGCGCGCGGGG': 1, \
            'ACCGTA': 3, 'GCTTTAAAAAGGCAA': 1, 'ACCGTAACCTTG': 2"

my_list = [x.strip() for x in my_string.split(",")]
for i in my_list:
    print i + "," # Added the comma as is is present in the form asked by the OP

As Istvan pointed out, you seem to have a dictionary, but this might provide an example of splitting and 'listifying' a string :)

Cheers

ADD COMMENT
0
Entering edit mode

Am I missing something or can you just do my_list = my_string.split(",")?

ADD REPLY
0
Entering edit mode

You could either use my_string.split(", ") #Note the space after the comma, or the form I used. Both will get rid of the trailing space. Cheers

ADD REPLY
2
Entering edit mode
14.1 years ago

The string that you seem to have is a dictionary printed out directly on screen. The second output could be generated by something like:

mydict = {'ACC': 2, 'ACACAGTTTGCGCGCGCGGGG': 1, 'ACCGTA': 3, 'GCTTTAAAAAGGCAA': 1, 'ACCGTAACCTTG': 2}

print mydict

print '-----'

for key, value in mydict.items():
   print '%s:%s,' % (key, value)

this will print:

{'ACC': 2, 'ACACAGTTTGCGCGCGCGGGG': 1, 'ACCGTAACCTTG': 2, 'ACCGTA': 3, 'GCTTTAAAAAGGCAA': 1}
-----
ACC:2,
ACACAGTTTGCGCGCGCGGGG:1,
ACCGTAACCTTG:2,
ACCGTA:3,
GCTTTAAAAAGGCAA:1,
ADD COMMENT
1
Entering edit mode
13.7 years ago
Sai Liu ▴ 10
>>>a=''''ACC': 2, 'ACACAGTTTGCGCGCGCGGGG': 1, 'ACCGTA': 3, 'GCTTTAAAAAGGCAA': 1
, 'ACCGTAACCTTG': 2'''

>>> a.replace(',',',\n')
"'ACC': 2,\n 'ACACAGTTTGCGCGCGCGGGG': 1,\n 'ACCGTA': 3,\n 'GCTTTAAAAAGGCAA': 1,\n 'ACCGTAACCTTG': 2"

>>> print a.replace(',',',\n')
'ACC': 2,
'ACACAGTTTGCGCGCGCGGGG': 1,
'ACCGTA': 3,
'GCTTTAAAAAGGCAA': 1,
'ACCGTAACCTTG': 2
ADD COMMENT

Login before adding your answer.

Traffic: 2154 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6