0

i need to read a file in python. My Problem is, that the file has an alternating amount of columns and that there are comments at the end of each line. I want to get rid of the comments while I read the file and save the data in a array or something like that. I Have absolutely no idea how to do that. Can anyone of you help me? This is how the file looks like:

2.0 # mass

-2.0 2.0 1999 # xMin xMax nPoint

1 5 # first and last eigenvalue to print

linear # interpolation type

2 # nr. of interpolation points and xy declarations

-2.0 0.0

2.0 0.0

Noah Rsnr
  • 3
  • 1
  • 2

4 Answers4

0

Is your data stored in csv? If yes, then this solution should work (I havent tested it though). If it isnt csv, then you can tweak it to match your source:

import csv
data=[]
with open('C:\\data.csv', 'r') as csvfile:
    csvreader = csv.reader(csvfile, delimiter = ',')
    for row in csvreader:
        datarow=[]
        for col in row:
            if not col.startswith('#'):
                datarow.append(col)
        data.append(datarow)

Your datarow(array) will contain the final data, minus the comments. Let me know if it works!

javapyscript
  • 720
  • 8
  • 22
0

data.txt:

2.0 # mass

-2.0 2.0 1999 # xMin xMax nPoint

1 5 # first and last eigenvalue to print

linear # interpolation type

2 # nr. of interpolation points and xy declarations

-2.0 0.0

2.0 0.0

main.py:

#open file and write into "content"
with open('data.txt', 'r') as f:
    content = f.readlines()

datalines=[]

for line in content:

    # remove linebreaks
    newline = line.replace('\n','')

    # find start of comments
    location = newline.find('#')
    # if line has no comment location = -1
    if location >= 0:
        # write into "newline" without comment, remove whitespaces at start and end with strip
        newline = newline[0:location].strip()

    # only append if line is not empty
    if newline is not '':
        datalines.append(newline)

# print
print(datalines)

print:

['2.0', '-2.0 2.0 1999', '1 5', 'linear', '2', '-2.0 0.0', '2.0 0.0']
Jonas
  • 1,838
  • 4
  • 19
  • 35
0

If you want I wrote a python module IO that makes file read easy, allowing you to ignore comments, even in the middle of a line. I am developing it on my GitHub

data.txt

2.0 # mass

-2.0 2.0 1999 # xMin xMax nPoint

1 5 # first and last eigenvalue to print

linear # interpolation type

2 # nr. of interpolation points and xy declarations

-2.0 0.0

2.0 0.0

The python code consists of only 2 lines

In [1]: import IO
In [2]: data = IO.readfile("data.txt").tolist()   # Returns a numpy object instead

Warning: not all lines have the same shape
Most frequent lenght : 2 (4 counts) 
Check rows :  0  1  

As you can see the module even gives you a warning if the lines do not have the same number of elements (since I wrote this to read tabulated data)

The output is

In [3]: data
Out[3]: 
[[2.0],
 [-2.0, 2.0, 1999.0],
 [1.0, 5.0],
 [2.0, 0.0],
 [-2.0, 0.0],
 [2.0, 0.0]]

Unfortunately this does not work for strings, so you may wish to select the interpolation type with a number (i.e. linear = 1, quadratic = 2, cubic = 3 etc.)

meowmeow
  • 60
  • 8
0
l = []
with open('data.txt', 'r') as f:
    for line in f:
        l.append(line.split('#')[0].split())
print(l)

# Output:
# [[2.0], [-2.0, 2.0, 1999], [1, 5], [linear], [2], [-2.0, 0.0], [2.0, 0.0]]
Hu Xixi
  • 1,799
  • 2
  • 21
  • 29