Python 3 输入:需要循环、拆分、字符串索引和 'r'、'w'、'r+' 的文本文件操作
Python 3 Input: Text file manipulation requiring loops, splitting, string indexing and 'r', 'w' , 'r+'
我是 python 的初学者,我不确定在我的作业中我应该搜索什么。我试图找到一种方法来将文本文件操作为所需的输出格式。
问题如下:
"Write a program that reads the data from the text file called ‘DOB.txt’ and prints it out in two different sections."
DOB 文本文件读取(每行名称、姓氏和 DOB):
Orville Wright 21 July 1988
Rogelio Holloway 13 September 1988
Marjorie Figueroa 9 October 1988
Debra Garner 7 February 1988
Tiffany Peters 25 July 1988
Hugh Foster 2 June 1988
Darren Christensen 21 January 1988
Shelia Harrison 28 July 1988
Ignacio James 12 September 1988
Jerry Keller 30 February 1988
伪代码如下:
Read the file DOB.txt
create a loop that will go over the contents being read from the File
display every line being read ( this will have the name and the DOB of the
students)
Use string indexing to get the first name and the 2nd name
assign this to a variable and print the first name and surname
For example something like this first_name = name[0] + name[1]
Keep in mind the name is the iterator that will be looping over the file we
are reading too.
DO the same to the DOB ( use string indexing to get the DOB of the student )
Counter for numbering each name
counter for numbering each surname
open file for reading
store contents of file in the form of lines
loop through contents
split each line into words
access the word and charcaters using indexing
预期结果如下:
姓名:
- 奥维尔·赖特
- 罗杰里奥霍洛威
- 马乔里·菲格罗亚
- 黛布拉·加纳
- 蒂芙尼·彼得斯
出生日期:
- 1988 年 7 月 21 日
- 1988 年 9 月 13 日
- 1988 年 10 月 9 日
- 1988 年 2 月 7 日
- 1988 年 7 月 25 日
这里不是询问您的家庭作业的地方,尤其是在不展示您已经完成的工作的情况下 and/or 展示您难以处理的错误。
作业的一些提示:
正在打开和关闭 Python 中的文件。
我建议向下滚动到该部分的末尾并阅读有关 with
关键字的信息。示例:
with open("file.txt", "r") as f:
do_something_to_file()
而不是:
f = open("file.txt", "r")
do_something_to_file()
f.close()
拆分字符串。由于您正在从文本文件中将整行作为字符串读取,因此您需要将其拆分为名字、姓氏、出生日期等。来自 PyDocs 的 Definition of str.split()。 split()
returns 由您传递的字符分隔的元素列表,默认为空格。
>>> string = "hello world"
>>> string.split()
['hello', 'world']
您还可以指定要从字符串中拆分的最大元素数。要设置最多 3 个元素:
>>> string = "Today was a good day"
>>> string.split(" ", 2)
['Today', 'was', 'a good day']
您不需要为像您在问题中描述的那样简单的内容存储计数器。遍历要打印的姓名列表时,可以使用 enumerate
。 enumerate
returns 一个对象,其中包含您传递给它的可迭代对象(在您的例子中是一个列表)的每个元素,以及一个计数,默认情况下从 0 开始。示例:
>>> word_list = string.split(" ", 2)
>>> for i, word in enumerate(word_list, 1):
... print(f"{i}. {word}")
...
1. Today
2. was
3. a good day
我建议通过编辑将代码添加到您的问题中。您可以查看如何格式化问题的示例 here.
祝你任务顺利。
不确定我是否完全理解您的任务要求,但我得到的是:
- 您需要解析此文件的行
with open('dob.txt') as dob:
for row in dob:
#print(row) would show you you're actually printing the file's rows
- 您可能希望将此行中的单词拆分为一个列表。我将列表命名为 'rowstrings'
with open('dob.txt') as dob:
for row in dob:
rowstrings = row.split()
- 如果您尝试打印 'rowstring' 列表的元素,您会看到在每一行中,name 和 surname 元素位于此列表的位置 0 和 1,而 DOB 元素位于位置 2、3 和 4 .
with open('dob.txt') as dob:
for row in dob:
rowstrings = row.split()
name = rowstrings[0] + " " + rowstrings[1]
birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
- 为了获得计数器,我只需在循环外创建变量并执行以下操作:
namecounter = 0
surnamecounter = 0
with open('dob.txt') as dob:
for row in dob:
rowstrings = row.split()
name = rowstrings[0] + " " + rowstrings[1]
birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
namecounter += 1
surnamecounter += 1
要打印文件中所有行的姓名和生日,打印语句需要在 for 循环内。
namecounter = 0
surnamecounter = 0
with open('dob.txt') as dob:
for row in dob:
rowstrings = row.split()
name = rowstrings[0] + " " + rowstrings[1]
birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
namecounter += 1
surnamecounter += 1
print("Name\n" + name + "\n\n" + "Birth date\n" + birthdate)
由于您想要先打印姓名然后打印出生日期,因此您可以创建两个空列表并在其中一个列表中填写姓名,在另一个列表中填写出生日期。然后您可以遍历列表。
namelist = []
birthlist = []
namecounter = 0
surnamecounter = 0
with open('dob.txt') as dob:
for row in dob:
rowstrings = row.split()
name = rowstrings[0] + " " + rowstrings[1]
birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
namecounter += 1
surnamecounter += 1
namelist.append(name) #adds the string to the list
birthlist.append(birthdate)
for row in namelist: #prints each name in the namelist
print(row)
for row in birthlist: #prints each birthdate in the birthlist
print(row)
我是 python 的初学者,我不确定在我的作业中我应该搜索什么。我试图找到一种方法来将文本文件操作为所需的输出格式。
问题如下: "Write a program that reads the data from the text file called ‘DOB.txt’ and prints it out in two different sections."
DOB 文本文件读取(每行名称、姓氏和 DOB):
Orville Wright 21 July 1988
Rogelio Holloway 13 September 1988
Marjorie Figueroa 9 October 1988
Debra Garner 7 February 1988
Tiffany Peters 25 July 1988
Hugh Foster 2 June 1988
Darren Christensen 21 January 1988
Shelia Harrison 28 July 1988
Ignacio James 12 September 1988
Jerry Keller 30 February 1988
伪代码如下:
Read the file DOB.txt
create a loop that will go over the contents being read from the File
display every line being read ( this will have the name and the DOB of the
students)
Use string indexing to get the first name and the 2nd name
assign this to a variable and print the first name and surname
For example something like this first_name = name[0] + name[1]
Keep in mind the name is the iterator that will be looping over the file we
are reading too.
DO the same to the DOB ( use string indexing to get the DOB of the student )
Counter for numbering each name
counter for numbering each surname
open file for reading
store contents of file in the form of lines
loop through contents
split each line into words
access the word and charcaters using indexing
预期结果如下:
姓名:
- 奥维尔·赖特
- 罗杰里奥霍洛威
- 马乔里·菲格罗亚
- 黛布拉·加纳
- 蒂芙尼·彼得斯
出生日期:
- 1988 年 7 月 21 日
- 1988 年 9 月 13 日
- 1988 年 10 月 9 日
- 1988 年 2 月 7 日
- 1988 年 7 月 25 日
这里不是询问您的家庭作业的地方,尤其是在不展示您已经完成的工作的情况下 and/or 展示您难以处理的错误。
作业的一些提示:
正在打开和关闭 Python 中的文件。 我建议向下滚动到该部分的末尾并阅读有关
with
关键字的信息。示例:with open("file.txt", "r") as f: do_something_to_file()
而不是:
f = open("file.txt", "r") do_something_to_file() f.close()
拆分字符串。由于您正在从文本文件中将整行作为字符串读取,因此您需要将其拆分为名字、姓氏、出生日期等。来自 PyDocs 的 Definition of str.split()。
split()
returns 由您传递的字符分隔的元素列表,默认为空格。>>> string = "hello world" >>> string.split() ['hello', 'world']
您还可以指定要从字符串中拆分的最大元素数。要设置最多 3 个元素:
>>> string = "Today was a good day" >>> string.split(" ", 2) ['Today', 'was', 'a good day']
您不需要为像您在问题中描述的那样简单的内容存储计数器。遍历要打印的姓名列表时,可以使用
enumerate
。enumerate
returns 一个对象,其中包含您传递给它的可迭代对象(在您的例子中是一个列表)的每个元素,以及一个计数,默认情况下从 0 开始。示例:>>> word_list = string.split(" ", 2) >>> for i, word in enumerate(word_list, 1): ... print(f"{i}. {word}") ... 1. Today 2. was 3. a good day
我建议通过编辑将代码添加到您的问题中。您可以查看如何格式化问题的示例 here.
祝你任务顺利。
不确定我是否完全理解您的任务要求,但我得到的是:
- 您需要解析此文件的行
with open('dob.txt') as dob:
for row in dob:
#print(row) would show you you're actually printing the file's rows
- 您可能希望将此行中的单词拆分为一个列表。我将列表命名为 'rowstrings'
with open('dob.txt') as dob:
for row in dob:
rowstrings = row.split()
- 如果您尝试打印 'rowstring' 列表的元素,您会看到在每一行中,name 和 surname 元素位于此列表的位置 0 和 1,而 DOB 元素位于位置 2、3 和 4 .
with open('dob.txt') as dob:
for row in dob:
rowstrings = row.split()
name = rowstrings[0] + " " + rowstrings[1]
birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
- 为了获得计数器,我只需在循环外创建变量并执行以下操作:
namecounter = 0
surnamecounter = 0
with open('dob.txt') as dob:
for row in dob:
rowstrings = row.split()
name = rowstrings[0] + " " + rowstrings[1]
birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
namecounter += 1
surnamecounter += 1
要打印文件中所有行的姓名和生日,打印语句需要在 for 循环内。
namecounter = 0
surnamecounter = 0
with open('dob.txt') as dob:
for row in dob:
rowstrings = row.split()
name = rowstrings[0] + " " + rowstrings[1]
birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
namecounter += 1
surnamecounter += 1
print("Name\n" + name + "\n\n" + "Birth date\n" + birthdate)
由于您想要先打印姓名然后打印出生日期,因此您可以创建两个空列表并在其中一个列表中填写姓名,在另一个列表中填写出生日期。然后您可以遍历列表。
namelist = []
birthlist = []
namecounter = 0
surnamecounter = 0
with open('dob.txt') as dob:
for row in dob:
rowstrings = row.split()
name = rowstrings[0] + " " + rowstrings[1]
birthdate = rowstrings[3] + " " + rowstrings[2] + " " + rowstrings[4]
namecounter += 1
surnamecounter += 1
namelist.append(name) #adds the string to the list
birthlist.append(birthdate)
for row in namelist: #prints each name in the namelist
print(row)
for row in birthlist: #prints each birthdate in the birthlist
print(row)