python 零基础学习作业题求解
def file_display(file_name,line_num):line_num = line_num.strip()
(begin,end) = line_num.split(":")
if begin == " ":
begin = "1"
if end == " ":
end = "-1"
if begin == "1" and end == "-1":
promt = "全文"
elif begin == "1":
prompt = "从开始到第%s行"%end
elif end == "-1":
prompt = "从第%s行到结束"%begin
else:
prompt = "从第%s行到第%s行"%(begin,end)
print("\n文件%s%s的内容如下:\n"%(file_name,prompt))
file = open("F:/record.txt")
begin = int(begin) - 1
#begin = int(begin)
end = int(end) #把字符转换成数字类型
lines = end - begin
for i in range(begin):
file.readline() #此时指针指向了第begin行
if lines < 0:
print(file.read()) # read是读取剩余所有字符
else:
for j in range(lines):
print(file.readline(),end = " ")# readline是只读取一行
file.close()
#file_name = "F:/record.txt"
#line_num = ":"
file_name = input(r'请输入要打开的文件(C:\\test.txt):')
line_num = input('请输入需要显示的行数【格式如 13:21 或 :21 或 21: 或 : 】:')
file_display(file_name, line_num)
请输入要打开的文件(C:\\test.txt):F:/record.txt
请输入需要显示的行数【格式如 13:21 或 :21 或 21: 或 : 】::
文件F:/record.txt从第行到第行的内容如下:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-54e6c973cee7> in <module>
44 file_name = input(r'请输入要打开的文件(C:\\test.txt):')
45 line_num = input('请输入需要显示的行数【格式如 13:21 或 :21 或 21: 或 : 】:')
---> 46 file_display(file_name, line_num)
<ipython-input-19-54e6c973cee7> in file_display(file_name, line_num)
23
24 file = open("F:/record.txt")
---> 25 begin = int(begin) - 1
26 #begin = int(begin)
27 end = int(end) #把字符转换成数字类型
ValueError: invalid literal for int() with base 10: ''
错误原因是什么?如何修改? 这样试试:
def file_display(file_name, line_num):
line_num = line_num.strip()
(begin, end) = line_num.split(":")
if begin == "":
begin = "1"
if end == "":
end = "-1"
if begin == "1" and end == "-1":
promt = "全文"
elif begin == "1":
prompt = "从开始到第%s行" % end
elif end == "-1":
prompt = "从第%s行到结束" % begin
else:
prompt = "从第%s行到第%s行" % (begin, end)
print("\n文件%s%s的内容如下:\n" % (file_name, prompt))
file = open("F:/record.txt")
begin = int(begin) - 1
# begin = int(begin)
end = int(end)# 把字符转换成数字类型
lines = end - begin
for i in range(begin):
file.readline()# 此时指针指向了第begin行
if lines < 0:
print(file.read())# read是读取剩余所有字符
else:
for j in range(lines):
print(file.readline(), end=" ")# readline是只读取一行
file.close()
# file_name = "F:/record.txt"
# line_num = ":"
file_name = input(r'请输入要打开的文件(C:\\test.txt):')
line_num = input('请输入需要显示的行数【格式如 13:21 或 :21 或 21: 或 : 】:')
file_display(file_name, line_num) 错误原因是你没有输入东西,却还要int它,这样试试:
def file_display(file_name,line_num):
line_num = line_num.strip()
if not all(begin,end := line_num.split(":")):
return
if begin == " ":
begin = "1"
if end == " ":
end = "-1"
if begin == "1" and end == "-1":
promt = "全文"
elif begin == "1":
prompt = "从开始到第%s行"%end
elif end == "-1":
prompt = "从第%s行到结束"%begin
else:
prompt = "从第%s行到第%s行"%(begin,end)
print("\n文件%s%s的内容如下:\n"%(file_name,prompt))
file = open("F:/record.txt")
begin = int(begin) - 1
#begin = int(begin)
end = int(end) #把字符转换成数字类型
lines = end - begin
for i in range(begin):
file.readline() #此时指针指向了第begin行
if lines < 0:
print(file.read()) # read是读取剩余所有字符
else:
for j in range(lines):
print(file.readline(),end = " ")# readline是只读取一行
file.close() qiuyouzhi 发表于 2020-8-7 17:52
错误原因是你没有输入东西,却还要int它,这样试试:
有东西啊,begin,end不是赋值了吗? Bonnie_9999 发表于 2020-8-7 18:00
有东西啊,begin,end不是赋值了吗?
额 Bonnie_9999 发表于 2020-8-7 18:00
有东西啊,begin,end不是赋值了吗?
看看我那段代码行不行 https://fishc.com.cn/forum.php?mod=redirect&goto=findpost&ptid=177054&pid=4893240 Bonnie_9999 发表于 2020-8-7 18:00
有东西啊,begin,end不是赋值了吗?
赋值了,可它的内容是个空字符串 qiuyouzhi 发表于 2020-8-7 18:06
赋值了,可它的内容是个空字符串
明白了,谢谢!虽然小问题,但困扰我一天。找了个对比软件才发现我错在哪里。
页:
[1]