列表并从文件中读取逗号分隔的元素

时间:2018-08-16 14:24:49

标签: python-3.x list file

这是我的代码

#course registration
list_courses=[]
for line in open("courses.txt",'r').readlines():
    list_courses.append(line.strip())
print ("Gathering course information from file: \n",list_courses)
close("courses.txt")
list_student=[]
for line in open("students.txt",'r').readlines():
    list_student.append(line.strip())
print("Here is student info: \n",list_student)
close("students.txt")

这在尝试关闭文件时给我错误。我该如何关闭,基本上是在读取文件内容并将其存储在列表中。现在稍后要关闭打开的文件。出现错误。

我按照以下建议编辑了代码。 新代码是

list_courses=[]
with open("courses.txt",'r') as myfile1:
      list_courses=myfile1.readlines()
      list_courses=[x.strip() for x in list_courses]

print ("Gathering course information from file: \n",list_courses)

list_student=[]
with open("students.txt",'r') as myfile1:
      list_student=myfile1.readlines()
      list_student=[x.strip() for x in list_student]

print("Here is student info: \n",list_student)

courses.txt中的信息为

cs101,C programming
cs102,Digital logic and design
cs103,Electrical engineering
cs231,IT networks
cs232,IT Workshop
cs233,IT programming
cs301,Compilers and automata
cs302,Operating Systems
cs303,Networks
cs401,Game Theory
cs402,Systems Programming
cs403,Automata
ec101,Digitization
ec102,Analog cicuit design
ec103,IP Telephony
ec201,Wireless Network
ec202,Microwave engineering
ec203,Antenna
ec301,Maths2
ec302,Theory of Circuits
ec303,PCB design
ec401,PLC programming
ec402,Scada
ec403,VLSI

运行代码时,我得到输出

Gathering course information from file: 

     ['cs101,C programming', 'cs102,Digital logic and design', 'cs103,Electrical engineering', 'cs231,IT networks', 'cs232,IT Workshop', 'cs233,IT programming', 'cs301,Compilers and automata', 'cs302,Operating Systems', 'cs303,Networks', 'cs401,Game Theory', 'cs402,Systems Programming', 'cs403,Automata', 'ec101,Digitization', 'ec102,Analog cicuit design', 'ec103,IP Telephony', 'ec201,Wireless Network', 'ec202,Microwave engineering', 'ec203,Antenna', 'ec301,Maths2', 'ec302,Theory of Circuits', 'ec303,PCB design', 'ec401,PLC programming', 'ec402,Scada', 'ec403,VLSI']

相反,我想要的是来自courses.txt第一行的输入cs101,以便进入list_courses [0]和list_courses [1]以进行C编程,即 list_courses [0] = cs101 list_courses [1] = C编程

因此,我尝试了一些方法,其中programe采取了这一行,并读取了存储为该行的list元素的行,但是在courses中有一个逗号分隔了两个元素。txt和逗号分隔的值应为单独的list元素。 >

2 个答案:

答案 0 :(得分:0)

这将为您工作:

students=[]
subject=[]
with open("students.txt","r") as f:
    for line in f.readlines():
        eachl=line.split(",")
        students.append(eachl[0])
        subject.append(eachl[1][:-1])

您将获得两个包含学生姓名的列表,另一个包含主题的列表:

学生名单如下:

['cs101', 'cs102', 'cs103', 'cs231', 'cs232', 'cs233', 'cs301', 'cs302', 'cs303', 'cs401', 'cs402', 'cs403', 'ec101', 'ec102', 'ec103', 'ec201', 'ec202', 'ec203', 'ec301', 'ec302', 'ec303', 'ec401', 'ec402', 'ec40`3]

主题列表如下:

['C programming', 'Digital logic and design', 'Electrical engineering', 'IT networks', 'IT Workshop', 'IT programming', 'Compilers and automata', 'Operating Systems', 'Networks', 'Game Theory', 'Systems Programming', 'Automata', 'Digitization', 'Analog cicuit design', 'IP Telephony', 'Wireless Network', 'Microwave engineering', 'Antenna', 'Maths2', 'Theory of Circuits', 'PCB design', 'PLC programming', 'Scada', 'VLSI']

答案 1 :(得分:0)

你为什么不尝试

students = []
courses = []

open(“courses.txt”, “r”) as f
for line in f.readlines()
    a, b = line.split(“,”)
    students.append(a)
    courses.append(b[:-1])
f.close()

这将产生两个清单的学生和课程