简体   繁体   English

Python 3.4中的数据组织3

[英]Data Organizing in Python 3.4 3

In Python I am using this file Criteria.txt and it looks like this 在Python中,我正在使用此文件Criteria.txt,它看起来像这样

Homework,5,10,10

Quiz,3,20,20

Midterm,1,50,30

Final,1,100,40

I typed in this code to open and manipulate the Criteria.txt file 我输入此代码以打开和操作Criteria.txt文件

criteria=open('Criteria.txt','r')
criteria_line=criteria.readline()



for criteria_line in criteria:
      print (criteria_line),
      criteria_myline = criteria_line.strip().split(',')
      print ("Assignment Type: "+criteria_myline[0])
      print ("Number: "+ criteria_myline [1])
      print ("Points: "+ criteria_myline [2])
      print ("Weight: "+ criteria_myline [3])
      print ()

The result was this 结果是这样

(None,)

Assignment Type: Quiz

Number: 3

Points: 20

Weight: 20

Midterm,1,50,30

(None,)

Assignment Type: Midterm

Number: 1

Points: 50

Weight: 30

Final,1,100,40

(None,)

Assignment Type: Final

Number: 1

Points: 100

Weight: 40

This was all correct except Homework didn't show up. 这一切都是正确的,只是没有出现家庭作业。 You can see in the Criteria.txt file that Homework was the first assignment type in the list. 您可以在Criteria.txt文件中看到,作业是列表中的第一个分配类型。 What is wrong with this code? 此代码有什么问题? Why did it leave Homework out? 为什么将功课留在外面?

The line 线

criteria_line=criteria.readline()

reads the first line, the one containing Homework . 读取第一行,其中包含Homework In the following for loop, you immediately discard this first result. 在下面的for循环中,您将立即丢弃此第一个结果。

This happens because you read the first line of the file before your for loop starts. 发生这种情况是因为您 for循环开始之前先读取了文件的第一行。 This causes the cursor in the file object to move to the next line, so when you iterate through for criteria_line in criteria , you're excluding the very first line (the one that includes Homework ). 这将导致文件对象中的光标移动到下一行,因此,当您遍历for criteria_line in criteria ,您将排除第一行(包括Homework那一行)。 If you remove the line 如果删除线

criteria_line = criteria.readline()

it should work. 它应该工作。

This all works because a file object is an iterable and python will iterate over the lines in the file by default. 所有这些都有效,因为文件对象是可迭代的,并且python默认会遍历文件中的行。

When you criteria_line=criteria.readline() , you've already consumed one line from your file object criteria which is Homework,5,10,10 , a fix is to read whenever you need the data in the for loop and as side note use with context opening file for better & safer coding: 当您criteria_line=criteria.readline() ,您已经消耗了文件对象criteria中的Homework,5,10,10 ,即Homework,5,10,10 ,一种解决方法是在需要for循环中的数据时读取并作为旁注with上下文打开文件with使用, with获得更好和更安全的编码:

with open('Criteria.txt') as criteria    
    for criteria_line in criteria:
        #print (criteria_line) #for debugging
        criteria_myline = criteria_line.strip().split(',')
        print ("Assignment Type: "+criteria_myline[0])
        print ("Number: "+ criteria_myline [1])
        print ("Points: "+ criteria_myline [2])
        print ("Weight: "+ criteria_myline [3])
        print ()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM