简体   繁体   English

Python:readlines()方法创建空列表

[英]Python: readlines()-method creates empty lists

I'm trying to parse items from a text file with lines of text separated by semicolons like this: 我正在尝试使用分号分隔的文本行来解析文本文件中的项目,如下所示:

4037;HKO_2005;OBJECT-ORIENTED PROGRAMMING               ;18.12.2011;5

4037;HKO_2009;DATABASES I                               ;2.5.2011;5

4037;HKO_2011;ALGORITHMS I                              ;7.5.2011;5

4037;HKO_2038;PROGRAMMING BASICS IN JAVA                ;22.5.2010;5

to a list of lists like this: 到这样的列表列表:

['4037', 'HKO_2005', 'OBJECT-ORIENTED PROGRAMMING', '18.12.2011', '5'],
['4037', 'HKO_2009', 'DATABASES I', '2.5.2011', '5'],
['4037', 'HKO_2011', 'ALGORITHMS I', '7.5.2011', '5'],
['4037', 'HKO_2038', 'PROGRAMMING BASICS IN JAVA', '22.5.2010', '5']

Right now the code I'm using for testing looks like this: 现在,我用于测试的代码如下所示:

class Main:
    def inputFile(self):
        with open('data.txt', 'r') as data:
            self.stuff = data.readlines()
            self.separate = [elem.strip().split(';') for elem in self.stuff]
            print(self.separate)

justdoit = Main()
justdoit.inputFile()

My problem is what you already saw: the text file didn't look to have double newlines until I pasted it here. 我的问题是您已经看到的:文本文件在我粘贴到这里之前看起来没有双换行符。 Using my code the readlines()-method creates empty lists in between with the newlines like this: 使用我的代码,readlines()方法在两个换行符之间创建一个空列表,如下所示:

['4037', 'HKO_2005', 'OBJECT-ORIENTED PROGRAMMING          ', '18.12.2011', '5'],
[''],
['4037', 'HKO_2009', 'DATABASES I                          ', '2.5.2011', '5'],
[''],
['4037', 'HKO_2011', 'ALGORITHMS I                         ', '7.5.2011', '5'],
[''],
['4037', 'HKO_2038', 'PROGRAMMING BASICS IN JAVA           ', '22.5.2010', '5']
['']

I believe I can later strip the blanks from the course names with rstrip(), but the newlines are giving me a headache. 我相信以后可以使用rstrip()从课程名称中删除空格,但是换行符让我头疼。 Earlier I was getting an IndexError because of this and I had no idea the text file had double newlines. 早些时候,我因此而得到了IndexError,而且我不知道文本文件是否包含双换行符。 How can I effectively ignore or remove these extra newlines before the lists are created? 在创建列表之前,如何有效地忽略或删除这些多余的换行符?

您可以为列表理解添加条件:

self.separate = [elem.strip().split(';') for elem in self.stuff if elem.strip()]

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

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