fastest way convert tab-delimited file to csv in linux

前端 未结 11 1285
感情败类
感情败类 2020-12-04 07:56

I have a tab-delimited file that has over 200 million lines. What\'s the fastest way in linux to convert this to a csv file? This file does have multiple lines of header i

11条回答
  •  醉梦人生
    2020-12-04 08:21

    If you're worried about embedded commas then you'll need to use a slightly more intelligent method. Here's a Python script that takes TSV lines from stdin and writes CSV lines to stdout:

    import sys
    import csv
    
    tabin = csv.reader(sys.stdin, dialect=csv.excel_tab)
    commaout = csv.writer(sys.stdout, dialect=csv.excel)
    for row in tabin:
      commaout.writerow(row)
    

    Run it from a shell as follows:

    python script.py < input.tsv > output.csv
    

提交回复
热议问题