Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9137701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:09:35+00:00 2026-06-17T09:09:35+00:00

def ints(filename): a = [] f = open(filename, r) lines = f.readlines() f.close() for

  • 0
def ints(filename):
    a = []
    f = open(filename, "r")
    lines = f.readlines()
    f.close()
    for line in lines:
        numbers = line.split()
        for number in numbers:            
            a.append(int(number))
    return a

This is my function so far, I want to be able to read a file containing integers and characters like “x” and “b” etc and return a list of only the integers. At the moment the function can only deal with a file containing integers.

How can I modify this to exclude characters or letters?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:09:36+00:00Added an answer on June 17, 2026 at 9:09 am

    This is my edit of your code, which performs as you expect it to.

      def ints(filename):
            """A function that takes the filename of a file as an input arguement, computs and returns a list of
            integers of all the numbers in the file."""
            a = []
            f = open(filename, "r")
            lines = f.readlines()
            f.close()
            for line in lines:
                for character in line:
                    try:
                        a.append(int(character))
                    except ValueError:
                        pass
            return a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

@with_checker([int]) def check_sort(list_of_ints): self.assertTrue(isinstance(list_of_ints, list)) self.assertTrue(len(list) == len(qsort(list))) self.assertTrue(False) I'm trying to run this,
def download_if_dne(href, filename): if os.path.isfile(filename): # print 'already downloaded:', href return False else: if
def makecounter(): return collections.defaultdict(int) class RankedIndex(object): def __init__(self): self._inverted_index = collections.defaultdict(list) self._documents = []
I have a function: def test(ints: Int*) = ints.foreach(println(_)) How to convert Array[Int] or
def de_punctuate(xs): return re.findall(r[\w']+(?:-[\w']+)*, xs) numbers = partial(re.search, r'[0,9]') def no_numbers(xs): return filterfalse(numbers, xs)
I'm running this scala code on a 32-bit quad-core Core2 system: def job(i:Int,s:Int):Long =
import unittest from paycheck import with_checker class TestStrings(unittest.TestCase): @with_checker([int]) def test_sort(self, list_of_ints): self.assertTrue(isinstance(list_of_ints, list))
On 2.7.5.final, I'm attempting to add a Iterable list of Ints like so def
I want to change the numbers in my dictionary to int values for use
I can define a function as: def print(n:Int, s:String = blah) {} print: (n:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.