[Solved] How to extract values from tuple in Python?


As has been stated A is already a Python tuple, there is no need to convert it into a string first and attempt to parse using a regular expression.

If you are trying to write this data to a CSV file, the tuple can be written directly as follows:

import datetime 
import csv

A = ((248500353L,
    11,
    '4',
    '248741302',
    633,
    7321L,
    7321L,
    'EAD4083003',
    0,
    datetime.datetime(2011, 4, 19, 23, 0, 42),
    datetime.datetime(2011, 4, 19, 23, 1, 39)),)

with open('output.csv', 'wb') as f_output:  
    csv_output = csv.writer(f_output)
    csv_output.writerow(A[0])

This would give you an output file looking like:

248500353,11,4,248741302,633,7321,7321,EAD4083003,0,2011-04-19 23:00:42,2011-04-19 23:01:39

In this case, these is no need to first try and extract all of the entries. If you need finer control over the date format, then this could easily be added. Here it uses the default conversion to a string format.

It does though assume that you wish to output all of the fields.

solved How to extract values from tuple in Python?