-1

I am storing data from a file into a list to analyse it for patterns. My intention is to be able to read a txt or csv file. I am struggling to find a way to analyse the information from the original list that is stored from the file to identify integers within that list.

I have tried the .isdigit() builtin method but that is returning False although the 'data' list is numbers, comma's and a some string's. The 'data' list does not have string numbers just integers.

It works as expected until the noCheck function, which just prints the ValueError.

data = []
IntData = []

def analyse():
    try:
        openfile = input('Enter file path: ')
        with open(openfile) as f:
            read = f.read()
            data.append(read)
            print(data)
            next = input('Press any Key to continue..')
            if next == '':
                noCheck()
    except FileNotFoundError:
        print('No File Found')

def noCheck():
    for number in data:
        try:
            IntData.append(int(number))
            print(IntData)
        except ValueError:
            print('No numbers found')


analyse()

Any help is greatly appreciated

EDIT: This is the data in the txt file:

08,30,34,44,45,57\n09,18,20,42,43,46\n14,19,30,45,48,58\n02,04,17,28,37,45\n14,18,23,28,36,37\n05,08,40,41,43,45\n18,25,29,32,35,58\n04,10,11,17,53,54\n24,30,44,50,56,57

Barb
  • 427
  • 3
  • 14
  • can you see a sample of your .txt file or .csv file? – Manvir Aug 05 '19 at 19:48
  • Maybe you should put each line of the file into a separate element of `data`? You're putting the entire file into a single element. – Barmar Aug 05 '19 at 19:54
  • `data = read.split("\n")` – Barmar Aug 05 '19 at 19:55
  • And if it's a CSV file, you need to split each line at comma characters. Maybe use the `csv` module. – Barmar Aug 05 '19 at 19:56
  • Please see the edit for the data in the file – Barb Aug 05 '19 at 20:00
  • don't you think you are giving commas to int() function. – Jainil Patel Aug 05 '19 at 20:26
  • Yes I know, I want to parse the numbers from the all other characters in the list. My idea was passing the data through int function to drop all other characters from the list and store it in a new list so it can be passed through a function later on – Barb Aug 05 '19 at 20:32

2 Answers2

0

Your question is asking to identify if there are any integers in the lines being read. Your code is currently just casting the lines to integers, instead of checking if they are integers.

I don't know how your file looks like, but you can do:

for line in data:
    if(isinstance(line, int)):
        IntData.append(line)

You also want to check the contents of IntData at the very end of reading your file, instead of throwing an exception after each line.

You can simply add:

if (len(IntData) == 0):
    print("No integers")
else:
    print("There are integers")
alex067
  • 3,159
  • 1
  • 11
  • 17
0
data = []
IntData = []

def analyse():
    try:
        openfile="ex1data1.txt"
        with open(openfile) as f:
            read = f.read()
            data.append(read)
            print(data)
            next = input('Press any Key to continue..')
            if next == '':
                noCheck()
    except FileNotFoundError:
        print('No File Found')

def noCheck():
    for number in data:
        num=number.replace('\\n',',')
        numlist=num.split(',')
        for i in numlist:
            try:
                IntData.append(int(i))
                print(IntData)
            except ValueError:
                print('No numbers found')


analyse()

enter image description here

instead of giving int('2,3,4,\n') you have to give int(2)...

so use

print (re.findall("\d+", s))

data = []
IntData = []
import re


def analyse():
    try:
        openfile="ex1data1.txt"
        with open(openfile) as f:
            read = f.read()
            data.append(read)
            print(data)
            next = input('Press any Key to continue..')
            if next == '':
                noCheck()
    except FileNotFoundError:
        print('No File Found')

def noCheck():
    for i in data:
        for k in re.findall("\d+", i):
            IntData.append(k)


analyse()

for further information :Extract Number from String in Python

Jainil Patel
  • 1,284
  • 7
  • 16
  • Works great for the instances of comma's and \n – Barb Aug 05 '19 at 21:05
  • It is a solution to an example of knowing what the file contains, I would have to rewrite the code dependable upon the file that I am reading in case of instances that contain other characters other than \n or a comma. num.replace() only takes 3 arguments at most. I cannot pass a module through it only string so if the text contained anymore than 3 strings it would not parse to just integers – Barb Aug 05 '19 at 21:24