Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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_For Loop_Readfile - Fatal编程技术网

Python 用于读取循环,但需要跳过特定的范围值

Python 用于读取循环,但需要跳过特定的范围值,python,for-loop,readfile,Python,For Loop,Readfile,我正在尝试用Python编写一个程序来读取文本文件。文本文件如下所示: <br>12345,Ballpeen Hammer,25,18.75</br> 56789,Phillips Screwdriver,120,10.95 24680,Claw Hammer,35,15.98 13579,Box Wrench,48,20.35 28967,Hex Wrench,70,19.98 它读取文件并生成一个列表,这是我想要的,但我只想要以20000到79999的值开始

我正在尝试用Python编写一个程序来读取文本文件。文本文件如下所示:

<br>12345,Ballpeen Hammer,25,18.75</br>

56789,Phillips Screwdriver,120,10.95

24680,Claw Hammer,35,15.98

13579,Box Wrench,48,20.35

28967,Hex Wrench,70,19.98
它读取文件并生成一个列表,这是我想要的,但我只想要以20000到79999的值开始的行。我编写了一个名为“inventoryitem2”的类,它通过defstr传递数字并显示


我应该将该条件放在哪里,如何设置?

Python具有我建议您使用的本机支持(您的数据至少看起来像CSV)

您可以使用以下内容:

导入csv
下限=20000
上限=79999
项目=[]
打开('item_numbers.csv',换行='')作为csv文件:
item_reader=csv.reader(csvfile)
#环行
对于项_读取器中的行:
#CSV自动将行拆分为“`”上的字符串列表`
#所以我们只需要将第一个值--`row[0]`转换为int
#并将其与你的极限进行比较

如果(int(row[0])>=lower_limit)和(int(row[0])Python具有我建议您使用的本机支持(您的数据至少看起来像CSV)

您可以使用以下内容:

导入csv
下限=20000
上限=79999
项目=[]
打开('item_numbers.csv',换行='')作为csv文件:
item_reader=csv.reader(csvfile)
#环行
对于项_读取器中的行:
#CSV自动将行拆分为“`”上的字符串列表`
#所以我们只需要将第一个值--`row[0]`转换为int
#并将其与你的极限进行比较

如果(int(row[0])>=lower_limit)和(int(row[0]))不重新发明轮子(csv解析)使用:不重新发明轮子(csv解析)使用:很好,非常感谢。我只是在学习Python(到目前为止非常喜欢)并且收到了一个我刚通过的课程的项目。虽然课程结束了,但我仍然想理解OOP。我将尝试你建议的答案。很好,这将让我开始尝试完成我想要完成的事情。感谢你告诉我如何在for循环中放置条件。很好,非常感谢。我正在学习Python(到目前为止我非常喜欢)并收到了我参加的一个课程的一个项目,但几乎没有通过。虽然课程已经结束,但我仍然想理解OOP。我将尝试你建议的答案。很酷,这将让我开始尝试完成的工作。感谢你向我展示如何在for循环中放置条件。
import inventoryitem2
FILENAME = ('item_numbers.txt')
# Open the text file.
infile = open(FILENAME, 'r')


def main():

    current_numbers = current_list()
    #print(current_numbers)

    # Print using the class. 
    #items = inventoryitem2.InventoryItem2(item_number)
    #print(items)

# Function for getting item numbers.
def current_list():

    # Empty list.
    item_number = []

    for line in infile:
        item_numbers = line.split(',')[0]
        item_number.append(item_numbers)

    for numbers in item_number:
        print(numbers)


main()