How to run something on each line of txt file?

2024/11/17 22:24:23

I am trying to get this to run on each line of a .txt file.

D1 =int(lines[0])*10
D2 =int(lines[1])*9
D3 =int(lines[2])*8
D4 =int(lines[3])*7
D5 =int(lines[4])*6
D6 =int(lines[5])*5
D7 =int(lines[6])*4
D8 =int(lines[7])*3
D9 =int(lines[8])*2
D10=int(line[9])*1
Sum =(D1+D2+D3+D4+D5+D6+D7+D8+D9)
Mod = Sum % 11D11= 11 - Mod
if D11 == 10:D11 = 'X'

Basically, the text file contains ISBN-10 numbers that I need to verify by running this code on each of the numbers inside of the text file. Here is where lines is defined because the text file contains "-" and I need to remove those before i start the process shown above.

filename = input("Enter the name of the .txt file: ")
file = open(filename, "r")
lines = file.read()
if "-" in lines:lines = lines.replace("-", "")
lines = file.readline()
while lines:D1 =int(lines[0])*10D2 =int(lines[1])*9D3 =int(lines[2])*8D4 =int(lines[3])*7D5 =int(lines[4])*6D6 =int(lines[5])*5D7 =int(lines[6])*4D8 =int(lines[7])*3D9 =int(lines[8])*2D10=int(lines[9])*1Sum =(D1+D2+D3+D4+D5+D6+D7+D8+D9)Mod = Sum % 11D11= 11 - Modif D11 == 10:D11 = 'X'if D10 == D11:print("Your ISBN-10's are Valid!")else:print("Your ISBN-10's are invalid")
Answer

You can use a for loop:

for i,line in enumerate(textFile):#code to run on each line  

Where line is the value of the current line in the loop and i is the line number so let's say your text file was:

John
Doe

Then the for loop would be:

i as 0
line as John

on the first run and so on.

https://en.xdnf.cn/q/120152.html

Related Q&A

Python np.nditer() - ValueError: Too many operands

I have a few methods which pass different amount of messy data to this function to combine headers with data and return a list of dictionaries:def zip_data(self, indicator_names, indicator_values):valu…

Python 3 - Find the Mode of a List

def mode(L):shows = []modeList = []L.sort()length = len(L)for num in L:count = L.count(num)shows.append(count)print List = , LmaxI = shows.index(max(shows))for i in shows:if i == maxI:if modeList == []…

Create list of sublists [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…

Python error - list index out of range?

Anyone help me see why I keep getting "list index out of range" as an error ?? def printInfo(average):average.sort() # sorts the list of tuples average.reverse() # reverses the list of tu…

Access strings inside a list

A Python list has [12:30,12:45] and I want to access the 12:30 for the first iteration, and on the second iteration I should get 12:45.my_list=[12:30,12:45] for each_value in my_list:print(each_value[0…

How do I install NumPy under Windows 8.1?

How do I install NumPy under Windows 8.1 ? Similar questions/answers on overflow hasnt helped.

Design In-Memory File System - Python - Trie - Leetcode Error

I am trying to solve the below leetcode problem Design a data structure that simulates an in-memory file system. Implement the FileSystem class: FileSystem() Initializes the object of the system. List …

OpenTurns Error when trying to use kernel build [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

How to write data to excel with header using Pandas?

I want to write the excel file with the header isI have data and I want to fill out the excel file. Then the expected result isThis is my code but it does not show as my expected. Could you help me to …

Cooldown format in days discord.py

Im currently making a command which has a cooldown of 5 days, and Im currently using this code for the cooldown. def better_time(self, cd:int):time = f"{cd}s"if cd > 60:minutes = cd - (cd …