iron2man
iron2man

Reputation: 1827

Opening txt file, Invalid literal for float()

Just trying to open this data set in txt format.

400.,41.7693
403.977686150861,42.68129270405837
408.3434392432695,43.86688321933424
412.61217560029104,45.59966935704514
416.4928450157652,48.72324436844505
420.9556148435605,51.64162102143182
423.86611690516617,53.534005882352936
427.0676691729323,56.2927838121295
431.43342226534077,59.73555626994983
435.31409168081495,60.85274733242134
439.291777831676,64.52351796625626
442.68736357021584,67.25949607843137
446.56803298569,68.92388276333789
450.73975260732476,71.29506379388965
454.7174387581858,72.41225485636114
458.21004123211253,73.68904464204286
462.4787775891341,75.5130300501596
466.6504972107689,76.72142038303693
470.2401164200825,77.72461235750114
473.73271889400917,78.63660506155951
477.61338830948335,79.57139758321932
481.9791414018918,80.64298901048792

When trying to open in...

with open('SunData.txt') as I:
    data = I.read()
data = data.split('\n')
WaveData = [float(row.split()[0]) for row in data]
IntData = [float(row.split()[1]) for row in data]

I'm returned with this error,

invalid literal for float(): 400.,41.7693

How do I correct this error?

Upvotes: 1

Views: 175

Answers (4)

Essex
Essex

Reputation: 6138

If you want to read the txt file, you can use the numpy library and the command : np.loadtxt()

Then you can make lots of processes because you get a numpy array ;)

import numpy as np

reading = np.loadtxt("SunData.txt", delimiter=',', usecols=(0, 1), unpack=True)

print reading

enter image description here

The documentation is there : doc

Upvotes: 0

Ryan
Ryan

Reputation: 14649

for item in data.split('\n'):
    floats = item.split(',')

Upvotes: 1

Vivek Chavda
Vivek Chavda

Reputation: 473

This isn't really doing anything:

row.split()

Perhaps you meant

row.split(',')

Upvotes: 0

Moses Koledoye
Moses Koledoye

Reputation: 78554

Use row.split(',') to split on commas. .split() without passing arguments will only split on whitespaces.

Upvotes: 4

Related Questions