-1

I have a csv file full of data, which is all type string. The file is called Identifiedλ.csv.

Here is some of the data from the csv file:

['Ref', 'Ion', 'ULevel', 'UConfig.', 'ULSJ', 'LLevel', 'LConfig.', 'LLSJ']
['12.132', 'Ne X', '4', '2p1', '2P3/2', '1', '1s1', '1S0']
['12.132', 'Ne X', '3', '2p1', '2P3/2', '1', '1s1', '1S0']
['12.846', 'Fe XX', '58', '1s2.2s2.2p2.3d1', '4P5/2', '1', '1s2.2s2.2p3', '4S3/2']

What I would like to do is the read the file and search for a number in the column 'Ref', for example 12.846. And if the number I search matches a number in the file, print the whole row of that number .

eg. something like:

csv_g = csv.reader(open('Identifiedλ.csv', 'r'), delimiter=",") 

for row in csv_g:
       if 12.846 == (row[0]):
            print (row)

And it would return (hopefully)

['12.846', 'Fe XX', '58', '1s2.2s2.2p2.3d1', '4P5/2', '1', '1s2.2s2.2p3', '4S3/2']

However this returns nothing and I think it's because the 'Ref' column is type string and the number I search is type float. I'm trying to find a way to convert the string to float but am seemingly stuck.

I've tried:

df = pd.read_csv('Identifiedλ.csv', dtype = {'Ref': np.float64,})

and

array = b = np.asarray(array, 
        dtype = np.float64, order = 'C')

but am confused on how to incorporate this with the rest of the search.

Any tips would be most helpful! Thank you!

  • Even after converting the string to a float `==` may fail. See https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Selcuk Dec 05 '19 at 01:23
  • 3
    I'd never treat an ID as a number, let alone a floating point number. Why not just treat it as a string or specific object? It feels like falling in the same trap as Sun / Java did when they decided to treat the version number as float (only to find out that they were very restricted when it came to version numbers later on). – Maarten Bodewes Dec 05 '19 at 01:24
  • Why not `if 12.846 == float(row[0]):`? Wrapped in a `try`/`except ValueError` if the row doesn't always contain valid `float` strings. – ShadowRanger Dec 05 '19 at 01:25
  • 1
    @Selcuk: For a literal and an equivalent value parsed from a string with the same contents, the comparison will be safe. The problem only arises when one of the values was *computed* from other `float` values and doesn't result in the value you expect due to floating point representational issues. – ShadowRanger Dec 05 '19 at 01:26
  • I'm not sure that article helps me very much, sorry I'm new to python – Rosie Chow Dec 05 '19 at 01:27
  • If it is possible to save the CSV file without quotation marks around numbers, then you can use the [`QUOTE_NONNUMERIC`](https://docs.python.org/3/library/csv.html#csv.QUOTE_NONNUMERIC) option to read the numbers as floating point automatically. – Stuart Dec 05 '19 at 01:46
  • Can you share the data, as it appears in the file? I recommend using a context manager to handle the file IO. I agree entirely with @MaartenBodewes though, just because something contains digits doesn’t mean it should be a numeric type. Are you open to a solution using Pandas? What else is your program doing with the data? – AMC Dec 05 '19 at 02:43

1 Answers1

0

Python has a function to convert strings to floats. For example, the following evaluates to True:

float('3.14')==3.14

I would try this conversion while comparing the value in the first column.

Manju
  • 33
  • 4