Character counting...



Can anyone simplify this for me?  

I don't know if it is going to work I tried to adjust the code to fit on the page. Might have to delete the extra `+\` 



```
def main():
    try:
        
        infile = open('text.txt','r')
        files = infile.readlines()

        lower,count,blank,dig = 0,0,0,0
    #Counting uppercase
        for line in files:
            count+=line.count('A')+line.count('B')+line.count('C')+line.count('D')+\ +line.count('E')+line.count('F')+line.count('G')+line.count('H')+\
+line.count('I')+line.count('J')+line.count('K')+line.count('L')+\
+line.count('M')+line.count('N')+line.count('O')+line.count('P')+\
+line.count('Q')+line.count('R')+line.count('S')+line.count('T')+\
+line.count('U')+line.count('V')+line.count('W')+line.count('X')+\
+line.count('Y')+line.count('Z')

        print 'There are',count,'uppercase letters.'
        print
    #Counting lowercase
        for line in files:
            lower+=line.count('a')+line.count('b')+line.count('c')+line.count('d')+\
+line.count('e')+line.count('f')+line.count('g')+line.count('h')+\
+line.count('i')+line.count('j')+line.count('k')+line.count('l')+\
+line.count('m')+line.count('n')+line.count('o')+line.count('p')+\
+line.count('q')+line.count('r')+line.count('s')+line.count('t')+\
+line.count('u')+line.count('v')+line.count('w')+line.count('x')+\
+line.count('y')+line.count('z')
        
        print 'I also have',lower,'lowercase letters.'
        print
    #Number of didgits
        for line in files:
            dig+=line.count('0')+line.count('1')+line.count('2')+line.count('3')+\
+line.count('4')+line.count('5')+line.count('6')+line.count('7')+\
+line.count('8')+line.count('9')
        print 'The number of digits in the file =',dig
        print
    #number of whitespace characters
        for line in files:
            blank+=line.count(' ')
        print 'I found',blank,'whitespaces in this file'
        print
        raw_input('Hit enter to quit')
        infile.close
    except:
        print 'An error occured.'
        
main()
```
I'm not going to go through all of your code because I'm in a hurry, but this should simplify it quite a bit. At a minimum it will make it easier to read.



```
alphabet = ['A','B','C','D'....etc]
count = 0
infile = open('infile.txt','r')
for character in infile.readlines():
    if character in alphabet:
        count += 1

print count
```
How you do this will depend on a number of factors: since you haven't tried to explain in any detail, I'll assume you know nothing, and just give you the basics.  

  

The easiest way to do this is to read the text file into a single string, and then use the .NET framework to find the match:  


```
Dim text As String = File.ReadAllText("D:\Temp\MyFile.txt")
Dim index As Integer = text.IndexOf("hello")
If index >= 0 Then
   ' String is in file, starting at character "index"
End If
```
  

There are other ways, but that is pretty much the simplest. `for i in count:` means `for i in 7:`, which won't work. The bit after the `in` should be of an iterable type, not a number. Try this:



```
for i in range(count):

``` [@cssc](/u/cssc), another option would be to take the regex way.

first, it finds the index of the 5th pipe.

then, it takes the substring until that index.

give this a try:

inputstr.substring(0, regex.matches(inputstr, "\|")(4).index + 1) Solution:


Hello Gavin,

You have got a nice question. No matter how the question is, don’t forget to ask. Thanks.  
There are a couple of ways to find the number in digits in a number. As you told you just started learning and well-known with the for-loop. I am trying to solve the problem using a simple iteration. Please have a look below program. I hope the program will clarify itself to you as you’ve covered the for-loop topic already.


```
public class DigitsInNumber{
    static int countDigit(long x){ 
        int counter = 0; 
        while (x != 0) { 
            x = x / 10; 
            ++counter; 
        } 
        return counter; 
    }

  	public static void main(String[] args){ 
        long x = 1052809467; 
        System.out.print("Number of digits : " + countDigit(x)); 
    } 
}
```
Thanks again. Good Day! i use a mac as well. you can set textedit to ‘plain text’ instead of ‘rich text’ and make sure it’s saving as a utf-8 .txt file in the preferences, but there isn’t an easy way to show the line numbers. if it’s saving as .rtf, that might be one reason your code isn’t working for you currently.

as fo… The existing solutions based on `findall` are fine for non-overlapping matches (and no doubt optimal except maybe for HUGE number of matches), although alternatives such as `sum(1 for m in re.finditer(thepattern, thestring))` (to avoid ever materializing the list when all you care about is the count) are also quite possible. Somewhat idiosyncratic would be using `subn` and ignoring the resulting string...:



```
def countnonoverlappingrematches(pattern, thestring):
  return re.subn(pattern, '', thestring)[1]

```

the only real advantage of this latter idea would come if you only cared to count (say) up to 100 matches; then, `re.subn(pattern, '', thestring, 100)[1]` might be practical (returning 100 whether there are 100 matches, or 1000, or even larger numbers).


Counting **overlapping** matches requires you to write more code, because the built-in functions in question are all focused on NON-overlapping matches. There's also a problem of definition, e.g, with pattern being `'a+'` and thestring being `'aa'`, would you consider this to be just one match, or three (the first `a`, the second one, both of them), or...?


Assuming for example that you want possibly-overlapping matches *starting at distinct spots in the string* (which then would give TWO matches for the example in the previous paragraph):



```
def countoverlappingdistinct(pattern, thestring):
  total = 0
  start = 0
  there = re.compile(pattern)
  while True:
    mo = there.search(thestring, start)
    if mo is None: return total
    total += 1
    start = 1 + mo.start()

```

Note that you do have to compile the pattern into a RE object in this case: function `re.search` does not accept a `start` argument (starting position for the search) the way *method* `search` does, so you'd have to be slicing thestring as you go -- definitely more effort than just having the next search start at the next possible distinct starting point, which is what I'm doing in this function. hello,

there are more possibilities how write output to file. first is by standard methods of jscript by “scripting.filesystemobject”.

var fso = new activexobject(“scripting.filesystemobject”);

var forreading = 1;

var forwriting = 2;

if (fso.fileexists(‘c:\test.txt’))

var f1 = fso.opentextfile… without a specific situation, you can trivially get paths count by doing:

list length > trim tree > list length …

(se hai tastiera italiana, puoi fare le parentesi graffe premendo alt dx + shift + il tasto delle quadre… ) c = input()  

t = len(c)  

temp = ""  

for i in range(0,len(c)):  

    if t<3:  

        break  

    else:  

        if i in (0,1,t-2,t-1):  

            temp = temp+c[i]  

        else:  

            continue  

print(temp) not [full disclosure] a linux person per se.

something similar to the following should work:

https://www.unix.com/shell-programming-and-scripting/68166-converting-integer-string.html

start simple with just enough code to test the concept.

see if it works and modify/correct as necessary.

then if it still does not work, then post your documented code accordingly along an explanation of what is or is not happening as you expect or desire.