문제

I have a txt file that I would like to alter so I will be able to place the data into columns see example below. The reason behind this is so I can import this data into a database / array and perform calculations on them. I tried importing/pasting the data into LibreCalc but it just imports everything into one column or it opens the file in LibreWriter I'm using ubuntu 10.04. Any ideas? I'm willing to use another program to work around this issue. I could also work with a comma delimited file but I'm not to sure how to convert the data to that format automatically.

Trying to get this:
WAVELENGTH,   WAVENUMBER,   INTENSITY,    CLASSIFICATION,     CODE,
1132.8322,    88274.326,     2300,        PT II,   9356- 97630,       05,

Here's a link to the full file. pt.txt file

도움이 되었습니까?

해결책 4

The easiest way turned out to be importing using a fixed width like tohuwawohu suggested Thanks

Without transforming it to a comma-separated file, you could access the csv import options by simply changing the file extension to .csv (maybe you should remove the "header" part manually, so that only the columns heads and the data rows do remain). After that, you can try to use whitespace as column delimiter, or even easier: select "fixed width" and set the columns manually. – tohuwawohu Oct 20 at 9:23

다른 팁

Try this:

sed -e "s/(\s+)/,$1/g" pt.txt

is this what you want?

awk 'BEGIN{OFS=","}NF>1{$1=$1;print}' pt.txt

if you want the output format looks better, and you have "column" installed, you can try this too:

awk 'BEGIN{OFS=", "}NF>1{$1=$1;print}' pt.txt|column -t

The awk and sed one-liners are cool, but I expect you'll end up needing to do more than simply splitting up the file. If you do, and if you have access to Python 2.7, the following little script will get you going.

# -*- coding: utf-8 -*-

"""Convert to comma-delimited"""

import csv
from os import path
import re
import sys


def splitline(line):
    return re.split('\s{2,}', line)


def main():
    srcpath = path.abspath(sys.argv[1])
    targetpath = path.splitext(srcpath)[0] + '.csv'

    with open(srcpath) as infile, open(targetpath, 'w') as outfile:
        writer = csv.writer(outfile)
        for line in infile:
            if line.startswith('  '):
                line = line.strip()
                cols = splitline(line)
                writer.writerow(cols)


if __name__ == '__main__':
    main()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top