从python中的文本文件中读取和操作数据

时间:2018-03-09 04:01:43

标签: python string

我创建了一个读取文件并将第一行与其余行分开的函数。 我用于此的两个文件是aaa.txtbbb.txt。我试图操纵数据。我尝试了每个字符串的每个元素,但我意识到我需要漂浮它,但我不确定为什么它不会浮动。我哪里出错了?

aaa.txt位于

之下
test a line 1

3,6,8,99,-4,0.6,8

0,9,7,5,7,9,5

2,2,2,2,2,2,5

7,5,1,2,12,8,0.9

=====================

bbb.txt如下所示

test b line 1

1,2,3,4,5,6,7,8

55,0,90,09,1,2,3,

8,9,7,6,8,7,6

3,43,5,8,2,4,1

======================

def mi_func(P):
    f=open(P, 'r')
    first = f.readline()
    restlines= f.readlines()
    f.close()
    return first, restlines


afirst,arest = mi_func('aaa.txt')
bfirst,brest = mi_func('bbb.txt')

print(arest)
print(brest)

##################
#convert a rest into a float here
#convert b rest into a float here
#################
for i in range(len(arest)):
    arest[i] = [float(x) for x in arest[i].strip().split(',')]

for i in range(len(brest)):
    brest[i] = [float(x) for x in brest[i].strip().split(',')]


p=[i**2 for i in arest]             #square each element of a rest
c=[i**2 for i in brest]             #square each element of b rest
print(p)
print(c)
['3,6,8,99,-4,0.6,8\n', '0,9,7,5,7,9,5\n', '2,2,2,2,2,2,5\n', '7,5,1,2,12,8,0.9\n']
['1,2,3,4,5,6,7,8\n', '55,0,90,09,1,2,3,\n', '8,9,7,6,8,7,6\n', '3,43,5,8,2,4,1']

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-36603657d444> in <module>()
     21 
     22 for i in range(len(brest)):
---> 23     brest[i] = [float(x) for x in brest[i].strip().split(',')]
     24 
     25 

<ipython-input-1-36603657d444> in <listcomp>(.0)
     21 
     22 for i in range(len(brest)):
---> 23     brest[i] = [float(x) for x in brest[i].strip().split(',')]
     24 
     25 

ValueError: could not convert string to float: 

2 个答案:

答案 0 :(得分:1)

我发现你的错误,似乎你有一些新行字符和一些空字符&#39; 09 & #39; (在布雷斯特),你正试图施放到浮动

所以你需要先删除它们。使用您的代码只需几个简单的调整:

def mi_func(P):
    f=open(P, 'r')
    first = f.readline()
    restlines= f.readlines()
    f.close()
    return first, restlines


afirst,arest = mi_func('aaa.txt')
bfirst,brest = mi_func('bbb.txt')

#This code will remove leading 0's and split numbers into a list 
#while eliminating any standalone new line characters:

arest = [x.lstrip('0').split(',') for x in arest if x != '\n']
brest = [x.lstrip('0').split(',') for x in brest if x != '\n']

for i in range(len(arest)):
    arest[i] = [float(x)**2 for x in arest[i] if x != '\n' and x!= '']

print(arest, 'a')

for i in range(len(brest)):
    brest[i] = [float(x)**2 for x in brest[i] if x != '\n' and x != '']

print(brest, 'b')
# This is your output
#[[81.0, 49.0, 25.0, 49.0, 81.0, 25.0], [4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 25.0]] a
#[[3025.0, 0.0, 8100.0, 81.0, 1.0, 4.0, 9.0], [64.0, 81.0, 49.0, 36.0, 64.0, 49.0, 36.0]] b

答案 1 :(得分:0)

您可以使用此功能从您提供的文本文件中返回第一行浮动列表。

def mi_func(file_name):
    with open(file_name) as f:
        lines = f.readlines()
        float_list = [
            float(float_str)
            for line in lines[1:]
                for float_str in line.strip().split(',')
                    if float_str != None
        ]
        return lines[0].strip(), float_list
print(mi_func('aaa.txt'))
print(mi_func('bbb.txt'))