I have a .txt(data.txt) file containing csv data like:

X         Class
15.0001   Yes
18.00     NO
17.07     Yes

I need to make a function to return a list of tuples of each samples. So far I did:

import csv
def readAllData(str): 
    with open(str,'r') as f:
        data=[tuple(line) for line in csv.reader(f)]
    return (data)



I want:

[(15.001, Yes), (18.00, No), (17.07, Yes)]

Don't bring everything into memory at once...

It's won't work for many cases as well..

csv's are , separated files and tsv's are tab(\t) separated.. They fall in the same category though..

tqdm is optional..

import csv
from tqdm import tqdm_notebook

with open('sample.txt','rb') as tsvin, open('new.csv', 'wb') as csvout:

    tsvin = csv.reader(tsvin, delimiter='\t')
    csvout = csv.writer(csvout)

    for row in tqdm_notebook(tsvin):
            csvout.writerows([row[:] for _ in range(2)])

Technically speaking, you do not have a CSV, you have a TSV. So I would take the time to split out each row accordingly and then build your tuple.

In the line:

data=[tuple(line) for line in csv.reader(f)] return (data)

You need to replace csv.reader to handle tsv instead.

  • $\begingroup$ Can you give a little more explanation on how can I do that? $\endgroup$ – Ishrak Alaxander Hasin Nov 1 '18 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.