Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 18906

How do I get the full number? [duplicate]

$
0
0

I am extremely new to python, and I have been trying to wrap my head on what I need to do here. I'm trying to write some code where I do the following:

  • Open a file
  • Create a loop where I read through the file one line at a time.
  • Inside your loop, you will increment varTotal (a variable) by the value of the id (a number that appears at the beginning of each line in the file) of any line that contains a keyword
  • If more than 1 keyword appears in a line, only count the id once.

So far my code is as follows:

keywords = "ckneale8@weibo.com", "247.224.231.109"x, "Newborn", "205.36.91.89", "Madelin", "askeelqc@clickbank.net"varTotal = 0with open("if-file.txt","r") as file:    for line in file:        id = line[0]        print(line)        if any(kw in line for kw in keywords):            varTotal = varTotal + int(id)print(varTotal)

When I try to run it, I'm not getting the full number for id, only the first digit and I need the full number. Also, I think it is counting the same line more than once when I don't want it to.

Also, if it helps, I have tried messing around with the number(s) in the square bracket in the id = line[0]. (I.e. I have tried to do like id = line[0:3:1] and other variations, but I get an error due to the first few lines only contain a single number in the beginning of the line. The numbers in the beginning of each line in the file range from 1-1000)

I have tried changing the number(s) in the id = line[0] portion of the code.

Tried splitting the file in different ways

I've tried different variations of 'if keywords in file:#Do something'

Like I mentioned, I am new to python so I am not even sure what I am able to think of being able to do next, and am lost for ideas.


Viewing all articles
Browse latest Browse all 18906


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>