我需要在python中读取文件。我的问题是,该文件具有交替的列数,并且每行末尾都有注释。在读取文件并将数据保存在数组或类似内容中时,我想摆脱注释。我完全不知道该怎么做。你们谁能帮我吗? 文件是这样的:
2.0#质量
-2.0 2.0 1999#xMin xMax nPoint
1 5#要打印的第一个和最后一个特征值
线性#插值类型
2#nr。插值点和xy声明
-2.0 0.0
2.0 0.0
答案 0 :(得分:0)
您的数据存储在csv中吗?如果是,则此解决方案应该有效(尽管我尚未对其进行测试)。如果不是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)
您的数据行(数组)将包含最终数据,减去注释。让我知道它是否有效!
答案 1 :(得分: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)
打印:
['2.0', '-2.0 2.0 1999', '1 5', 'linear', '2', '-2.0 0.0', '2.0 0.0']
答案 2 :(得分:0)
如果需要,我编写了一个python模块IO
,该模块使文件的读取变得容易,甚至在一行的中间,也可以忽略注释。我正在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
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
如您所见,如果行中元素的数量不同(自从我编写此代码是为了读取列表数据以来),该模块甚至会向您发出警告
输出为
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]]
不幸的是,这不适用于字符串,因此您可能希望选择带数字的插值类型(例如,线性= 1,二次方= 2,三次方= 3,等等)
答案 3 :(得分: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]]