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

Python 读取文本文件并指定值

Python 读取文本文件并指定值,python,python-2.7,Python,Python 2.7,我有一个包含5列的文本文件: StudentID用户id FirstName LastName昵称 012233477263约翰·史密斯·约翰尼 8273263 8349734蒂默·詹森·蒂姆 我试图用python读取这个文件,并将每个值分配给单独的变量。到目前为止,我已经成功地读到了台词。但是,我无法指定值 迄今为止的代码: StudentID = [] UserID = [] FirstName = [] LastName = [] NickName = [] with open(tex

我有一个包含5列的文本文件:

StudentID用户id FirstName LastName昵称
012233477263约翰·史密斯·约翰尼
8273263 8349734蒂默·詹森·蒂姆
我试图用python读取这个文件,并将每个值分配给单独的变量。到目前为止,我已经成功地读到了台词。但是,我无法指定值

迄今为止的代码:

StudentID = []
UserID = []
FirstName = []
LastName = []
NickName = []

with open(textfile,'r') as f:
    lines = f.readlines()
这是一种非常奇怪的写作方式。更好的方法是创建一个班级,让学生了解学生的属性以及如何从中阅读

class Student:
    def __init__(self, student_id=0, user_id=0, first_name='', last_name='', nickname=''):
        self._student_id = student_id
        self._user_id = user_id
        ...

    def from_line(self, line):
        values = line.split(' ')
        self._student_id = int(values[0])
        self._user_id = int(values[1])
        ...

f = open(textfile, 'r')
students = []
for line in f:
    students.append(Student())
    students[-1].from_line(line)
这是一种非常奇怪的写作方式。更好的方法是创建一个班级,让学生了解学生的属性以及如何从中阅读

class Student:
    def __init__(self, student_id=0, user_id=0, first_name='', last_name='', nickname=''):
        self._student_id = student_id
        self._user_id = user_id
        ...

    def from_line(self, line):
        values = line.split(' ')
        self._student_id = int(values[0])
        self._user_id = int(values[1])
        ...

f = open(textfile, 'r')
students = []
for line in f:
    students.append(Student())
    students[-1].from_line(line)
注意:对于列表的命名,您使用的是驼峰大小写约定。在Python中,根据定义变量的
PIP8
,应该使用小写字母,用下划线
分隔。camecase变量用于定义类

建议:

由于这些值属于同一个对象,因此您应该按照或的建议维护单个对象列表,只需存储
list
列表即可

例如:

persons = []
with open(textfile,'r') as f:
    for line in f.readlines():
        persons.append(line.split())

print persons
# [['0122334', '7727263', 'John', 'Smith', 'Johnny'],
#  ['8273263', '8349734', 'timmer', 'Jansen', 'tim']]
这里您将分别在
0
1
2
3
4
5
索引中有
StudentID
UserID
FirstName
LastName
昵称

注意:对于列表的命名,您使用的是驼峰大小写约定。在Python中,根据定义变量的
PIP8
,应该使用小写字母,用下划线
分隔。CamelCase变量用于定义类

建议:

由于这些值属于同一个对象,因此您应该按照或的建议维护单个对象列表,只需存储
list
列表即可

例如:

persons = []
with open(textfile,'r') as f:
    for line in f.readlines():
        persons.append(line.split())

print persons
# [['0122334', '7727263', 'John', 'Smith', 'Johnny'],
#  ['8273263', '8349734', 'timmer', 'Jansen', 'tim']]

这里有
StudentID
UserID
FirstName
LastName
昵称
分别位于
0
1
2
3
4
5
索引。您可以一次提取所有这些索引:

with open(textfile,'r') as f:
    columns = [line.split() for line in f.readlines()]
然后从
列创建每个列表

StudentID, UserID, firstNAme, LastNAme, NickName = map(list, zip(*columns))

首先创建一个列表列表,它是一个矩阵,由给定的列逐行分隔。然后,它通过为每列创建一个单独的列表来重新组合(映射)它们。

您可以一次提取所有它们:

with open(textfile,'r') as f:
    columns = [line.split() for line in f.readlines()]
然后从
列创建每个列表

StudentID, UserID, firstNAme, LastNAme, NickName = map(list, zip(*columns))

首先创建一个列表列表,它是一个矩阵,由给定的列逐行分隔。然后,它通过为每一列创建一个单独的列表来重新组合(映射)它们。

对于项目,zip中的集合(line.split(),[StudentID,UserID,FirstName,LastName,昵称]):collection.append(项目)
@Steven:将其添加为答案。这将对其他人有帮助。
对于项目,zip中的集合(line.split(),[StudentID,UserID,FirstName,LastName,昵称]):集合。追加(项目)
@Steven:将其作为答案添加。这将对其他人有所帮助。