1
$\begingroup$

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:
        f.readline()
        data=[tuple(line) for line in csv.reader(f)]
    return (data)

Output:

[('15.001\tYES',),
 ('18.00\tNo',),
 ('17.07\tYes',),]

I want:

[(15.001, Yes), (18.00, No), (17.07, Yes)]
$\endgroup$
0
1
$\begingroup$

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)])
$\endgroup$
2
$\begingroup$

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.

$\endgroup$
1
  • $\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.