简体   繁体   English

在python中读取没有注释的文件

[英]Reading a file with without comments in python

i need to read a file in python. 我需要在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 2.0 1999 # xMin xMax nPoint -2.0 2.0 1999#xMin xMax nPoint

1 5 # first and last eigenvalue to print 1 5#要打印的第一个和最后一个特征值

linear # interpolation type 线性#插补类型

2 # nr. 2号 of interpolation points and xy declarations 插值点和xy声明

-2.0 0.0 -2.0 0.0

2.0 0.0 2.0 0.0

Is your data stored in csv? 您的数据存储在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: 如果不是csv,则可以对其进行调整以匹配您的源:

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. 您的datarow(array)将包含最终数据,减去注释。 Let me know if it works! 让我知道它是否有效!

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

main.py: 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. 如果需要,我编写了一个python模块IO ,使文件易于阅读,即使在一行的中间,也可以忽略注释。 I am developing it on my GitHub 我正在我的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 python代码仅包含2行

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 (ie linear = 1, quadratic = 2, cubic = 3 etc.) 不幸的是,这不适用于字符串,因此您可能希望选择带数字的插值类型(即线性= 1,二次方= 2,三次方= 3,依此类推)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM