Python-从txt文件访问数据
我的目标是让一个程序请求测试平均值的输入并将其写入txt文件,第二个程序使用循环将tests.txt文件从第一个程序读取并处理到一个两列表格中,该表格显示测试名称和分数,精确到小数点后一位 读取txt文件的第二个程序是什么样子的? 以下是我的第一个程序代码:Python-从txt文件访问数据,python,file,text,Python,File,Text,我的目标是让一个程序请求测试平均值的输入并将其写入txt文件,第二个程序使用循环将tests.txt文件从第一个程序读取并处理到一个两列表格中,该表格显示测试名称和分数,精确到小数点后一位 读取txt文件的第二个程序是什么样子的? 以下是我的第一个程序代码: def main(): outfile =open('test.txt', 'w') print('Entering six tests and scores') num1 = float(input('Enter
def main():
outfile =open('test.txt', 'w')
print('Entering six tests and scores')
num1 = float(input('Enter % score on this test '))
num2 = float(input('Enter % score on this test '))
num3 = float(input('Enter % score on this test '))
num4 = float(input('Enter % score on this test '))
num5 = float(input('Enter % score on this test '))
num6 = float(input('Enter % score on this test '))
outfile.write(str(num1) + '\n')
outfile.write(str(num2) + '\n')
outfile.write(str(num3) + '\n')
outfile.write(str(num4) + '\n')
outfile.write(str(num5) + '\n')
outfile.write(str(num6) + '\n')
outfile.close()
main()
还有我的第二个节目:
def main():
infile = open('test.txt' , 'r')
line1 = infile.readline()
line2 = infile.readline()
line3 = infile.readline()
line4 = infile.readline()
line5 = infile.readline()
line6 = infile.readline()
infile.close()
line1 = line1.rstrip('\n')
line2 = line2.rstrip('\n')
line3 = line3.rstrip('\n')
line4 = line4.rstrip('\n')
line5 = line5.rstrip('\n')
line6 = line6.rstrip('\n')
infile.close()
main()
首先,绝对没有必要像那样重复你自己——一个简单的循环可以避免你编写如此重复的代码。不过,您可能需要考虑使用字典,因为这是一种数据结构,这种情况下需要将键(名称)映射到值(分数)。此外,您可能需要考虑使用<>代码> 语句作为上下文管理器,因为它将在嵌套的代码块之后自动关闭文件。p> 因此,考虑到所有这些因素,应该采取以下措施:
def first():
print('Entering six tests and scores')
my_tests = {}
for i in range(6):
name, score = input('Enter name and score, separated by a comma: ').split(',')
my_tests[name] = round(float(score), 1)
with open('tests.txt', 'w') as f:
for name, score in my_tests.items():
f.write('{} {}\n'.format(name, score))
…当涉及到问题的第二部分时:
def second():
with open('tests.txt', 'r') as f:
tests = f.readlines()
for row in tests:
test = row.rstrip('\n').split()
print('{}\t{}'.format(test[0], test[1]))
首先,绝对没有必要像那样重复你自己——一个简单的循环可以避免你编写如此重复的代码。不过,您可能需要考虑使用字典,因为这是一种数据结构,这种情况下需要将键(名称)映射到值(分数)。此外,您可能需要考虑使用<>代码> 语句作为上下文管理器,因为它将在嵌套的代码块之后自动关闭文件。p> 因此,考虑到所有这些因素,应该采取以下措施:
def first():
print('Entering six tests and scores')
my_tests = {}
for i in range(6):
name, score = input('Enter name and score, separated by a comma: ').split(',')
my_tests[name] = round(float(score), 1)
with open('tests.txt', 'w') as f:
for name, score in my_tests.items():
f.write('{} {}\n'.format(name, score))
…当涉及到问题的第二部分时:
def second():
with open('tests.txt', 'r') as f:
tests = f.readlines()
for row in tests:
test = row.rstrip('\n').split()
print('{}\t{}'.format(test[0], test[1]))
那么到底是什么问题呢?您可以将.txt中的所有行与:[x代表i in]放在一起,为每个测试分数添加名称有什么提示吗?这两个函数看起来都不是这样的。你也许可以把它浓缩成几行,但我不确定我能在脑海中描绘出你的过程。你能举例说明输入和输出吗?一旦你发现自己像以前那样复制/粘贴代码行,你应该停下来质疑是否有一个
for
循环方法作为基础,然后从那里获得更复杂的(如果必要的话)。DRY:不要重复你自己。为什么你需要一个中间文件?那么到底是什么问题呢?你可以将.txt中的所有行与[x代表i in]放在一起。关于为每个测试分数添加名称的任何提示?这两个函数看起来都不是这样的。你也许可以把它浓缩成几行,但我不确定我能在脑海中描绘出你的过程。你能举例说明输入和输出吗?一旦你发现自己像以前那样复制/粘贴代码行,你应该停下来质疑是否有一个for
循环方法作为基础,然后从那里获得更复杂的(如果必要的话)。DRY:不要重复你自己。为什么你需要一个中间文件?另外,请注意@mentita使用和来打开文件,而不是直接f=open(…)
。使用
上下文管理器将文件打开包装在一个中,为您处理异常和文件句柄关闭(如果文件不存在,您不必捕获,也不必担心调用f.close()
)此外,请注意@mentalita使用与
一起打开文件,而不是直接的f=open(…)
。使用
上下文管理器在中包装打开的文件可以为您处理异常和文件句柄关闭(如果文件不存在,您不必捕获,也不必担心调用f.close()
)