LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-06-2020, 12:13 PM   #1
kwatts60
LQ Newbie
 
Registered: May 2020
Posts: 4

Rep: Reputation: Disabled
How to display a CSV file in a nice format?


I have a python program that issues the LINUX "gedit" command to open up a csv file for display.
The gedit command opens up a window and displays the file, but the output is hard to read.

Is there some other editor that can use to open up a csv file and display it nicely?
 
Old 05-06-2020, 12:23 PM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
I prefer formatting CSV with miller and/or browsing them with VisiData, but I'm not a GUI guy.

Last edited by shruggy; 05-07-2020 at 01:06 AM.
 
1 members found this post helpful.
Old 05-06-2020, 12:27 PM   #3
sevendogsbsd
Senior Member
 
Registered: Sep 2017
Distribution: FreeBSD
Posts: 2,252

Rep: Reputation: 1011Reputation: 1011Reputation: 1011Reputation: 1011Reputation: 1011Reputation: 1011Reputation: 1011Reputation: 1011
If you are looking for a graphical application: Libreoffice, Gnumeric, probably many others.
 
Old 05-06-2020, 12:29 PM   #4
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Quote:
Originally Posted by shruggy View Post
I prefer formatting CSV with miller or browsing them with VisiData, but I'm not a GUI guy.
Both look good to me and I've added them to my toolkit. Cheers
 
Old 05-06-2020, 01:06 PM   #5
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Quote:
I have a python program that issues the LINUX "gedit" command to open up a csv file for display.
If you are using python then use python.
https://docs.python.org/3/library/csv.html
 
Old 05-06-2020, 03:44 PM   #6
remmilou
Member
 
Registered: Mar 2010
Location: Amsterdam
Distribution: MX Linux (21)/ XFCE
Posts: 211

Rep: Reputation: 69
You should definitely give "tad" a try. Tad = tabular data viewer.
https://www.tadviewer.com/
I use it for ridiculously big (multiple millions of lines) csv files and it runs smoothly.
 
Old 05-06-2020, 08:57 PM   #7
kwatts60
LQ Newbie
 
Registered: May 2020
Posts: 4

Original Poster
Rep: Reputation: Disabled
I decided to go with Libreoffice. It's like Excel for LINUX.
Works ok with CSV files but it has an annoying import dialog box.
I guess I can live with it for now.
Thanks for your help.
 
Old 05-07-2020, 10:22 PM   #8
WideOpenSkies
Member
 
Registered: May 2019
Location: /home/
Distribution: Arch Linux
Posts: 166

Rep: Reputation: 61
Have you considered using awk?
 
Old 05-08-2020, 01:54 AM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
@Contrapak Doing it right with awk would require using FPAT though, which is a feature exclusive to gawk.
 
Old 05-08-2020, 02:46 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
I would have though LibreOffice was a massive over-reach - a sledgehammer nut-cracker.
And really, for a site-specific problem what is the issue with using gawk ?. I long ago gave up fretting about POSIX compliance - I need solutions that work in my environment, not some "feel-good" resolution. Anything that makes my life easier is good. Gotta get into miller tho', I love the look of that as I've said before.
 
Old 05-08-2020, 04:03 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by shruggy View Post
@Contrapak Doing it right with awk would require using FPAT though, which is a feature exclusive to gawk.
Covering all the cases is quite difficult and benefits from a parser. Consider the following line:

Code:
1, "b", "c1,c2", "d1,d2,d3", "e", 6,7, 8,"i",9,"""j""", 11, """l,m""", 13,14
With a tab-delimited file or certain types of comma-separated values, it is doable with AWK. In some cases it is even easy. But when you start getting quotes and commas in the data itself, it quickly becomes a very difficult task for AWK.
 
Old 05-08-2020, 10:55 AM   #12
WideOpenSkies
Member
 
Registered: May 2019
Location: /home/
Distribution: Arch Linux
Posts: 166

Rep: Reputation: 61
Quote:
Originally Posted by Turbocapitalist View Post
With a tab-delimited file or certain types of comma-separated values, it is doable with AWK. In some cases it is even easy. But when you start getting quotes and commas in the data itself, it quickly becomes a very difficult task for AWK.
Perhaps you can use a combination of substitution and sed commands to get around the tabs and quotes? From your sample, it seems like separating by commas is possible.
 
Old 05-08-2020, 11:03 AM   #13
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
@Contrapak. Sorry, but sed is even worse in this respect than awk.
Quote:
Originally Posted by Contrapak View Post
From your sample, it seems like separating by commas is possible.
Ok, let's try it. Here is a real parser (csvtool):
Code:
$ csv='1, "b", "c1,c2", "d1,d2,d3", "e", 6,7, 8,"i",9,"""j""", 11, """l,m""", 13,14'
$ csvtool transpose - <<<"$csv"
1
b
"c1,c2"
"d1,d2,d3"
e
6
7
8
i
9
"""j"""
11
"""l,m"""
13
14
I challenge you to do the same with
Quote:
a combination of substitution and sed commands
@syg00. I made an example that demonstrates how miller compares to awk.

Last edited by shruggy; 05-08-2020 at 11:45 AM.
 
Old 05-08-2020, 03:49 PM   #14
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Answering my own challenge . Well, it is possible to do this particular transformation on this particular set of input data with sed. Moreover, throwing wc and pr into the mix, I could do it even on a multi-line set. Not that I would really like doing it in this manner:
Code:
#!/bin/bash
line(){ printf =%.0s {1..76};echo;}
csv='1, "b", "c1,c2", "d1,d2,d3", "e", 6,7, 8,"i",9,"""j""", 11, """l,m""", 13,14'
csv="$csv
$(rev <<<"$csv")"
line
printf %s\\n "$csv"
line
echo
echo == csvtool ==
csvtool transpose - <<<"$csv"

echo
echo == wc + sed + sed + pr ==
nr=$(wc -l <<<"$csv")
sed -E 's/("+)[ \t]*,/\1,/g;s/,[ \t]*("+)/,\1/g;s/,"/\n"/g;s/",/"\n/g' <<<"$csv"|
sed '/^[^"]/{s/^[ \t]*//;s/[ \t]*$//;s/[ \t]*,[ \t]*/\n/g}'|pr -$nr -T -s,

Last edited by shruggy; 05-09-2020 at 04:14 AM.
 
Old 05-09-2020, 04:47 AM   #15
fatmac
LQ Guru
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: Mainly Devuan, antiX, & Void, with Tiny Core, Fatdog, & BSD thrown in.
Posts: 5,485

Rep: Reputation: Disabled
Quote:
....command to open up a csv file for display.
The sc (spreadsheet calculator) program can do that for you.
 
  


Reply

Tags
csv



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to print lines in csv file if 1 csv column field = "text". There are 10 column (;) in csv file nexuslinux Linux - Newbie 9 04-22-2016 11:35 PM
[SOLVED] A challenging script - Replace field of CSV file based on another CSV file arbex5 Programming 11 06-12-2013 06:56 AM
[SOLVED] How to script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 12:45 PM
Map 1 CSV's columns to matching columns in another CSV 2legit2quit Programming 7 10-27-2011 08:53 AM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration