from sys import argv
script, input_file = argv
hello = open(input_file)
read1 = hello.readline()
print("Line1: ", read1, end = "")
line1 = len(read1) + 1
print(line1)
read2 = hello.readline()
print("Line2: ", read2, end = "")
line2 = len(read1 + read2) + 2
print(line2)
read3 = hello.readline()
print("Line3: ", read3, end = "")
line3 = len(read1 + read2 + read3) + 3
print(line3)
read4 = hello.readline()
print("Line4: ", read4)
beep = input("""I give you the option to choose the line you print:
1. Type: first line
2. Type: second line
3. Type: third line
4. Type: fourth line
""")
if (beep == "first line"):
choice = 0
elif (beep == "second line"):
choice = line1
elif (beep == "third line"):
choice = line2
else:
choice = line3
def runner(f):
hello.seek(f)
green = hello.readline()
print("The line reads: ", green)
runner(choice)
< /code>
Я хочу распечатать определенную строку из текстового файла, как указано пользователем. Я знаю, что функция seek () делает вход в терминах байтов для перемещения указателя чтения в позиции, соответствующей размеру в байтах. Например, если текстовый файл имеет 4 строки, а каждая строка имеет размер 10 байтов (включая символ Newline), то размер вход 11 соответствует началу второй строки. Now if I want to move the reading position to the beginning of the fourth line I will input 31 (sum of sizes of previous lines + 1) to the seek() function to move the reading/writing pointer to the fourth line.
But in the attached piece of code I have input sum of the sizes of previous 3 lines + 3 to get the position of the beginning of the fourth line (instead of sum of sizes of previous 3 lines + 1, which makes more sense), but still the code runs perfectly хорошо.
На самом деле, когда я заменяю +3 (или +2) на +1, я не получаю необходимую линию.>
Подробнее здесь: https://stackoverflow.com/questions/794 ... t-file-usi
Это правильный способ перейти к началу каждой строки в текстовом файле с помощью seek ()? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Это правильный способ перейти к началу каждой строки в текстовом файле с помощью seek ()?
Anonymous » » в форуме Python - 0 Ответы
- 5 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Это правильный способ перейти к началу каждой строки в текстовом файле с помощью seek ()?
Anonymous » » в форуме Python - 0 Ответы
- 2 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Preg_match() не соответствует началу строки после разделения строки nl2br() на
Anonymous » » в форуме Php - 0 Ответы
- 24 Просмотры
-
Последнее сообщение Anonymous
-