Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将文件读入列表-编辑_Python - Fatal编程技术网

Python将文件读入列表-编辑

Python将文件读入列表-编辑,python,Python,编辑-我似乎在计算测试零件数量时出错: lines=len(file.readlines()) N=行-2 当我在一个单独的脚本中测试它时,它确实起了作用 这里的初学者需要帮助。我的python脚本有一个问题,我自己无法解决。脚本应该从文本文件中读取浮点并进行一些计算。我无法将数字输入列表R[] 下面是一个文本文件示例,其中第一行(s[0])是标称值,第二行(s[1])是公差,以下几行是一些电阻器 3300.0 10.0 3132.0 3348.5 3557.3 3467.4 3212.0

编辑-我似乎在计算测试零件数量时出错:

lines=len(file.readlines())
N=行-2

当我在一个单独的脚本中测试它时,它确实起了作用

这里的初学者需要帮助。我的python脚本有一个问题,我自己无法解决。脚本应该从文本文件中读取浮点并进行一些计算。我无法将数字输入列表
R[]

下面是一个文本文件示例,其中第一行(
s[0]
)是标称值,第二行(
s[1]
)是公差,以下几行是一些电阻器


3300.0

10.0

3132.0

3348.5

3557.3

3467.4

3212.0

3084.6

3324.0

我有以下代码:

R = []
Ntoolow = Nlow = Nhigh = Ntoohigh = 0.0
lines = 0

def find_mean(q):
    tot = 0.0
    c = len(q)
    for x in range (c):
        tot += q[x]
        return tot/c


def find_median(q):
    c = len(q)
    if c%2:
        return float(q[int(c/2)])
    else:
        c /= 2
        return (q[int(c)]+q[int(c-1)])/2.0


file_name = input("Please enter the file name: ")
file = open(file_name, "r")

s = file.readlines()

Rnom = float(s[0])
Tol = float(s[1])


keepgoing = True

while keepgoing:
    s = file.readline()
    if s == "":
        keepgoing = False
    else:
        R.append(float(s))


lines = len(file.readlines())
N = lines - 2
R.sort()


Rmin = R[0]
Rmax = R[N-1]

Rlowerlimit = Rnom - Tol
Rupperlimit = Rnom + Tol


for rn in R:
    if rn < Rlowerlimit:
        Ntoolow += 1
    elif rn < Rnom:
        Nlow += 1
    elif rn <= Rupperlimit:
        Nhigh += 1
    else:
        Ntoohigh += 1


Ptoolow = 100.0 * Ntoolow / N
Plow = 100.0 * Nlow / N
Phigh = 100.0 * Nhigh / N
Ptoohigh = 100.0 * Ntoohigh / N


Rmean = find_mean(R)
Rmedian = find_median(R)


print("Total number of parts tested: " + str(N))
print("The largest resistor is: " + str(Rmax) + " and the smallest is: " + str(Rmin))
print("The mean is: " + str(Rmean) + " and the median is: " + str(Rmedian))
print("The percentage of resistors that have too low tolerance is: " + str(Ptoolow) + "%")
print("The percentage of resistors that have low tolerance is: " + str(Plow) + "%")
print("The percentage of resistors that have high tolerance is: " + str(Phigh) + "%")
print("The percentage of resistors that have too high tolerance is: " + str(Ptoohigh) + "%")

file.close()
R=[]
Ntoolow=Nlow=Nhigh=Ntoohigh=0.0
直线=0
def find_平均值(q):
tot=0.0
c=len(q)
对于范围(c)内的x:
tot+=q[x]
返回总成本
def find_中值(q):
c=len(q)
如果c%2:
返回浮动(q[int(c/2)])
其他:
c/=2
返回(q[int(c)]+q[int(c-1)])/2.0
文件名=输入(“请输入文件名:”)
文件=打开(文件名为“r”)
s=file.readlines()
Rnom=浮动(s[0])
Tol=浮动(s[1])
继续=正确
继续进行时:
s=file.readline()
如果s==“”:
继续=错误
其他:
R.附加(浮动)
lines=len(file.readlines())
N=行-2
R.sort()
Rmin=R[0]
Rmax=R[N-1]
Rlowerlimit=Rnom-Tol
Rupperlimit=Rnom+Tol
对于R中的rn:
如果rnelif rn不需要重新发明轮子,Python知道当它到达文件末尾时,您不需要添加自己的子句(另外,检测“”是否可能以空行结尾?不太确定)

请尝试以下代码:

with open(file_name) as f:
   try: 
       R = float(f.readlines())
   except TypeError:
       pass

不需要重新发明轮子,Python知道当它到达文件末尾时,您不需要添加自己的子句(另外,检测“”是否可能以空行结尾?不太确定)

请尝试以下代码:

with open(file_name) as f:
   try: 
       R = float(f.readlines())
   except TypeError:
       pass

Python有许多库,可以从CSV文件(如CSV、numpy和pandas)中读取信息。请尝试以下操作:

import csv
with open('filename.csv','r') as f:
    reader = csv.reader(f)
然后,您可以像这样迭代行:

for row in reader:
    print(row)

Python有许多库,可以从CSV文件(如CSV、numpy和pandas)中读取信息。请尝试以下操作:

import csv
with open('filename.csv','r') as f:
    reader = csv.reader(f)
然后,您可以像这样迭代行:

for row in reader:
    print(row)
替换你的代码

while keepgoing:
     s = file.readline()
     if s == "":
         keepgoing = False
     else:
         R.append(float(s))
有,

for i in s:
    R.append(float(i))
它给了我答案

这是我编辑代码后的输出

Total number of parts tested: -2
The largest resistor is: 3348.5 and the smallest is: 10.0
The mean is: 1.11111111111 and the median is: 3300.0
The percentage of resistors that have too low tolerance is: -200.0%
The percentage of resistors that have low tolerance is: -0.0%
The percentage of resistors that have high tolerance is: -50.0%
The percentage of resistors that have too high tolerance is: -200.0%
替换你的代码

while keepgoing:
     s = file.readline()
     if s == "":
         keepgoing = False
     else:
         R.append(float(s))
有,

for i in s:
    R.append(float(i))
它给了我答案

这是我编辑代码后的输出

Total number of parts tested: -2
The largest resistor is: 3348.5 and the smallest is: 10.0
The mean is: 1.11111111111 and the median is: 3300.0
The percentage of resistors that have too low tolerance is: -200.0%
The percentage of resistors that have low tolerance is: -0.0%
The percentage of resistors that have high tolerance is: -50.0%
The percentage of resistors that have too high tolerance is: -200.0%

跳过空行并删除换行符
\n
,方法是:


跳过空行并删除换行符
\n
,方法是:


我不明白你的问题。。。是否要将返回数字添加到列表R[]中?出现了什么问题?你有错误吗?如果有,是哪一个错误?无法回答你的问题。。。是否要将返回数字添加到列表R[]中?出现了什么问题?你有错误吗?如果有,是哪一个错误?
TypeError:float()参数必须是字符串或数字
^catch如下
,open(文件名)为f:try:R=float(f.readlines()),除了TypeError:pass
谢谢大家,更改了它
TypeError:float()参数必须是字符串或数字
^如下捕获这些
,open(文件名)为f:try:R=float(f.readlines())除了TypeError:pass
谢谢大家,更改了它如何防止将前两个数字(3300.0和10.0)添加到列表中?不想添加前两个数字吗?(3300.0和10.0)?如果您不想添加前两位数字(3300.0和10.0),意味着您应该像这样从第二个元素开始您的文本文件,因为s[2:]中的i:答案已被用户接受。那么,谁把它设为负值?我如何防止它将前两个数字(3300.0和10.0)添加到列表中?您不想添加前两个数字?(3300.0和10.0)?如果您不想添加前两位数字(3300.0和10.0),意味着您应该像这样从第二个元素开始您的文本文件,因为s[2:]中的i:答案已被用户接受。那么谁把它定为阴性?