CSV cleanup: remove all in col after (

0

Problem

Given a file with:

56 (65%), 33(75%), , 44, "“hello”,(5) world", goodbye
89 (25%), 33(75%), , 44, "hello world", goodbye
92 (97%), 33(75%), , 44, "hello world", goodbye

Goal

Write back to that file with:

56, 33, , 44, "“hello”,(5) world", goodbye
89, 33, , 44, "hello world", goodbye
92, 33, , 44, "hello world", goodbye

Example implementation [Python 518 bytes]

import csv
from sys import argv
from itertools import takewhile, imap


with open(argv[1], 'r+') as f0:
    r = csv.reader(f0, delimiter=',', quotechar='"')
    w = csv.writer(f0, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    rows = tuple(imap(lambda line: ''.join(
        '{}\0'.format(''.join(
            takewhile(lambda ch: ch != '(', col))).rstrip() for col in line), r))
    f0.seek(0)
    tuple(imap(lambda line: w.writerow(line.split('\0')), rows))

A T

Posted 2017-03-11T11:31:19.640

Reputation: 159

Question was closed 2017-03-11T15:45:44.137

Are we required to respect the full CSV specification, including quoted values, or are we allowed to skip that part? – John Dvorak – 2017-03-11T11:59:58.310

Yes, support for quoted values must be respected. – A T – 2017-03-11T12:03:15.433

Ah, drat. I would have been able to do it in 12 bytes otherwise. – John Dvorak – 2017-03-11T12:03:48.937

Aww :P - so close – A T – 2017-03-11T12:04:12.490

1I can't see why quoted values must be respected, as I'm given a file that does not contain them. Maybe you should specify better the task. – edc65 – 2017-03-11T13:26:05.777

Fixed. Extra kudos if you remove only parenthesised numerical with/without % from the CSV. – A T – 2017-03-11T14:05:36.493

If we have to support quoted values, you should add a test case where they matter, e.g., one with a quoted comma. What should happen if the brackets are quoted? – Dennis – 2017-03-11T15:39:21.553

Also, how much whitespace can occur before the opening bracket? Does all of it have to be removed? – Dennis – 2017-03-11T15:41:27.280

Finally, will the opening bracket always have a mtaching closing bracket? – Dennis – 2017-03-11T15:45:12.693

Updated examples. Parentheses are guaranteed to be balanced. – A T – 2017-03-12T00:19:08.077

Would you mind providing a more detail description of the problem? I see what the output is but I am assuming the steps in between... many thanks! – Chromozorz – 2017-03-12T03:36:45.710

Answers

1

Perl: 15 Bytes +1 for -p option

s/ ?\([^,"]+//g

Usage:

perl -pe 's/ ?\([^,"]+//g' file.csv

Toto

Posted 2017-03-11T11:31:19.640

Reputation: 909