Reading a file with without comments in python

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Reading a file with without comments in python



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





Perhaps use the "#" as a delimiter
– Tom
Aug 10 at 10:37





Split on # and take first part.
– Austin
Aug 10 at 10:39


#





Use regular expressions.
– Lev Zakharov
Aug 10 at 10:40





Possible duplicate of Python: How to ignore #comment lines when reading in a file
– Lev Zakharov
Aug 10 at 10:41





string - "2.0 2.0 1999 # xMin xMax nPoint" do, string[:string.index('#')] so this will eliminate all the characters post` '#'`
– Surya Tej
Aug 10 at 10:42


string - "2.0 2.0 1999 # xMin xMax nPoint"


string[:string.index('#')]




4 Answers
4


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]]



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!



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']



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


IO



data.txt


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.)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard