Home > front end >  how to identify the occurrence of first two characters of a number from a text file?
how to identify the occurrence of first two characters of a number from a text file?

Time:04-05

I have a text file that contains the names of people and their telephone numbers.

I want to identify the number of people with the telephone number beginning with 85.

The file looks like this :
\`EMAIL, NAME, LAST_NAME, TEL, CITY
, Ivan, Abramov, 7776514, Moscow
, Alexey, Nikolaev, 1348520, Moscow
, Marina, Shapar, 1111110, Moscow
, Dmitriy, Vasilyev, 0000000, Kazan
, Ekaterina, Ilyina, , St.Petersburg
, Anastasiya, Grigoryan, 1928421, Ekaterinburg
, Andrey, Fedorov, 85212384, Minsk
, Alexey, Lisitsyn, 1239532, Tver
, Dariya, Abramova, 7163908, Moscow
, Alexandr, Evdokimov, 482, Volokolamsk
, Nataliya, Kostina, 9031433, Moscow
, Nikolay, Ermolin, 8539233, St.Petersburg
, , , , 
, Vladimir, Solovovo, 4758395, St.Petersburg
, Vladimir, Ivanov, 4827594, Novosibirsk
, Sergey, Nikolaev, 1294375, Sarov
, Ivan, Ivanov, 8532354, Kazan
, Konstantin, Semenov, 8532286, Moscow
, Grigoriy, Smirnov, 3249235, Kaliningrad
, Vasiliy, , 7123465, Moscow
, Alexandr, Lashko, 9548324, St.Petersburg
, sdfsdf, dwfef, 9994532, Rostov
, Oksana, Edger, 8548433, Kostroma
, Olga, Bogomolova, 8953253, Moscow
, Evgeniy, Strizh, 1123313, Krasnodar
, Valentina, Sergach, 12345567, St.Petersburg
, Polina, Volskaya, 1394542, Vladimir
, Fedor, Malinin, 9453223, Moscow
, Olga, Pahomova, 8543231, Moscow
, ffff, ffff, ffff, fffff
, Stepan, Bogdanov, 4564624, St.Petersburg
, Dmitriy, Smirnov, 9355r24, Kazan
, Valeria, Kolokolkina, 12O4352, St.Petersburg
, Ivan, Sobolev, 2350223, Kirov
, Kristina, Orlova, 1342349, Talnakh
, Alexandr, Ermolin, 2345234, Mozhaysk
, Ekaterina, Vasilevskaya, 9583573, Moscow
, Tatiana, Koroleva, 8674532, Vidnoe
, Elena, Moskvina, 7584294, Moscow
, , , 8954245, Moscow
, Mikhail, Sinev, 5964353, St.Petersburg
, NO_NAME, NO_NAME, 2856735, Kaliningrad
, Anastasiya, Boshich, 9654534, Omsk
, Svetlana, Kapustina, 9476353, Moscow
, Boris, Nikolaev, 9583583, Moscow
, Andrey, Mozgvin, 8537572, Krasnodar
, Oksana, Bogatyreva, 8743565, St.Petersburg
, Kseniya, Nosatenko, 1234194, Yaroslavl
, fwefwgwgwfds, sfewrfw, 9584356, Pskov
, Konstantin, Grigoriev, 1284575, Moscow

Furthermore, I used this code, but it does not work. it keeps giving me zero though it must be 7.

f = open('task_file.txt' , 'r')
for line in f:
    if line.split(',')[3] != '':
        if line.split(',')[3][0:2] == '85' :
            a  = 1
print(a)
f.close()``
```

```

CodePudding user response:

with open('task_file.txt', 'r') as f:
    count = 0
    lines = f.readlines()
    for line in lines:
        line = line.strip().split()
        if line[3][0:2] == '85':
            count  = 1
# output: 7
print(count)

CodePudding user response:


f = open('task_file.txt', 'r')
text = f.read() # read the contents of file object
f.close() # we stored the text, can close the file
text = text.replace(' ', '') # get rid of spaces
text_list = text.split(',') # get a list of strings
n = 0
for t in text_list:
    if t[:2] == '85':
        n  = 1
print(n)

  • Related