簡體   English   中英

從python中的文本文件讀取和處理數據

[英]reading and manipulating data from a text file in python

我已經做了一個讀取文件並將第一行與其余行分開的函數。 我為此使用的兩個文件是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: 

我發現了您的錯誤,似乎您有一些換行符和一些空字符,以及試圖將其強制轉換為浮點數的' 09 '(布雷斯特)

因此,您需要先刪除它們。 對於您的代碼,這只是幾個簡單的調整:

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM