1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Python:Reading (int,float,comp,bool) from file

  1. Feb 4, 2017 #1
    1. The problem statement, all variables and given/known data
    Read a comma separated numeric (int, float, comp, bool) values from a file and store each
    comma separated value in a list
    . Sort the list (without using in-built sort/sorted function)
    and write it to output file in comma separated format.

    In my attempt I am not able to do the bold part in the question. That is if float value is there in file then it cannot be typecasted to int. How do I typecast with conditions?

    2. Relevant equations

    3. The attempt at a solution
    Code (Python):
    for line in f:
        for val in x:
           y.append(int(val))  # Here if float is stored in file then it cannot be typecasted to int.
    for i in range(len(y)-1):    # Doing bubble sort
        for j in range(len(y)-i-1):
            if y[j]>y[j+1]:
  2. jcsd
  3. Feb 4, 2017 #2


    Staff: Mentor

    Is the idea just to read values from the file and store them in a list? If so, why do you need to cast a floating point value to an integer value? The problem description you have doesn't mention doing this.
  4. Feb 4, 2017 #3
    But we have to sort, so we have to convert it into numeric type.
    EDIT: Ok, so we can compare them as strings only. It would compare lexicographically based on ASCII values but if we store them in file again, it is stored something like this-
    '1' , '2+j', '3.5', '4', 'FALSE', 'TRUE'
    This is storing the list contents in string format. I want output like this-
    1, 2+j, 3.5, 4, FALSE, TRUE
    without quotes
    For that I have to typecast finally?
    Last edited: Feb 4, 2017
  5. Feb 5, 2017 #4


    Staff: Mentor

    The problem is that the text file doesn't contain floats or integers or complex numbers -- it contains text. Your variable x is a list that contains strings. For example, if the first line of the input file is 3.5, 2, true, the list x will look like ['3.5', '2', 'true'].
    The reason that int(val) throws an exception (ValueError) is that int() can't be used on strings. For the same reason, float(), bool(), and complex() will fail when applied to strings.

    What you need to do for each value you get from the file is figure out if it's a complex or float or integer or Boolean. One way to do this is to have four other variables, with each initialized to a value of one of these four types (complex, float, integer, Boolean).
    Code (Python):

    v_cplex = 1+2j
    v_float = 1.5
    v_int = 1
    v_bool = True
    There's nothing special about the initializations I did above. I just want four variables with each of a specific type.

    For a given val, try assigning it to each of the above variables. If you assign val to f_float, for example, and get an error (ValueError), you know that val isn't a float value. After you determine that val is, say, a float, append v_float to your list y.

    Edit: One more thing. If you write the various values to a list as numbers, you won't be able to sort them, as complex numbers aren't comparable to real variables (such as int, float, bool).
    Last edited: Feb 5, 2017
  6. Feb 8, 2017 #5


    Staff: Mentor

    @Raghav Gupta, any progress on this problem?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted