lmaurits / prettytable

Automatically exported from code.google.com/p/prettytable
Other
22 stars 6 forks source link

from_csv should should take delimiter as kwarg #31

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

I'm currently using this lib to generate pretty ascii tables.

I get my data from a csv file where the delimiter is set to ;.

So I think thwe from_csv should take an optional delimiter arg, so it does not 
need to guess the dialect. I looked at the code and here is a corrected 
function to do this. (Only the first lines of the from_csv function)

def from_csv(fp, field_names = None, delimiter=None,**kwargs):                  

    if delimiter is None:                                                           
        dialect = csv.Sniffer().sniff(fp.read(1024))                                
        fp.seek(0)                                                                  
        reader = csv.reader(fp, dialiect)                                           
    else:                                                                           
        reader = csv.reader(fp, delimiter=delimiter)

the rest is the same

Original issue reported on code.google.com by wunio.zi...@gmail.com on 10 Jun 2013 at 8:43

GoogleCodeExporter commented 9 years ago
Hello.  Sorry for my very slow action on this.  I've just made a commit to 
trunk in the SVN repo implementing this.  Not only can you specifie a 
delimiter, you can pass any of the format parameters that csv.Reader accepts 
(delimiter, doublequote, escapechar, etc.).  It would be wonderful if you could 
give the new code a quick test on your scenario.

Btw, how was PrettyTable failing for you with ";" delimiters?  I tried using 
the old code on a file with ";" delims, and it worked just fine.

Original comment by luke@maurits.id.au on 5 Oct 2013 at 10:17