I writing a script in python in which I am extracting some information from some files and passing it into another file.
I have 2 dictionaries that contains lists of elements of a class, and I want to write each element of the list into a .csv file.
When I try to write the data to the file it appears in a weird way. I want the data to appear in this way:
Sample_3 45526536 7010285 31195850 7320401 45500691 7186449 31021951 7292291
Sample_1 48885783 7715654 33474067 7696062 48857972 7907873 33282945 7667154
but instead I have this result:
Sample_3 ['45526536'] ['7010285'] ['31195850'] ['7320401'] ['45500691'] ['7186449'] ['31021951'] ['7292291']
Sample_1 ['48885783'] ['7715654'] ['33474067'] ['7696062'] ['48857972'] ['7907873'] ['33282945']['7667154']
Does anyone know why is adding [´´] to the numbers? I try to make an really simple method to eliminate the [‘ ‘] but then I won’t print anything in the file.
def correct_str(value):
new= str(value).strip("['")
final= str(new).strip("']")
return final
Here is the code here I am adding elements to the dictionary, the dictionary is a global variable called stat_dict_l_u, and the name is the key to the dictionary, in this case Sample_1 and Sample_2, and list values is a list that contains the numbers I got from a file. I believe that maybe my problem comes from the way I am adding the values to the dictionary.
def processNumbersLeft(name,list_values):
list_reads=[]
for value in list_values:
print value.total_reads
list_reads.append(value.total_reads)
list_aligned_once=[]
for value in list_values:
n=re.split(" ",value.aligned_once)
list_aligned_once.append(n[0])
list_failed=[]
for value in list_values:
n=re.split(" ",value.failed)
list_failed.append(n[0])
list_aligned_several=[]
for value in list_values:
n=re.split(" ",value.aligned_several)
list_aligned_several.append(n[0])
stat_elem=Statistics(list_reads,list_aligned_once,list_failed,list_aligned_several)
stat_dict_l_u[name]=stat_elem
Thank you!
Try to use this case:
Using:
Note, if you need to a lot of work with csv-file, use helpful module csv. Example of using:
This example does exactly what you need. For more information read documentation.