This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Python Script To Split Dna Strings Into New Lines

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

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.

4 answers

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.

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

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

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

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,
>>>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

Log in to answer this question.