Reputation: 87
If this description is not enough, I can include a sample of my code to further see where my error is coming from. For the time being, I have a file that I am importing that looks like the following...
0.9,0.9,0.12
0.,0.75,0.16
0.7,0.75,0.24
0.7,0.75,0.32
0.5,0.,0.1
0.6,0.65,0.38
0.6,0.8,0.,
0.9,0.95,0.04
0.5,0.65,0.28
On the third to last row, there is a comma after the 0. of the last column, which looks like "0.,". Because of this, I get the error
Some errors were detected !
Line #7 (got 4 columns instead of 3)
for my code I use
%matplotlib notebook
import numpy as np #import python tools
import matplotlib.pyplot as plt
from numpy import genfromtxt
from mpl_toolkits.mplot3d import Axes3D
file = 'graph'
info = genfromtxt(file, delimiter=',')
beaming = info[:,0]
albedo = info[:,2]
diameter = info[:,1]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(diameter, albedo, beaming, c='r', marker='o')
ax.set_xlabel('diameter (km)')
ax.set_ylabel('albedo')
ax.set_zlabel('beaming parameter')
plt.show()
Maybe there is a way to ignore that comma and explicitly read in the first 3 columns or a way to get rid of the comma but i'm unsure. Any suggestions help!
Upvotes: 0
Views: 39
Reputation: 4033
You could remove all the commas and resave it as new file via:
with open("file.txt") as fi, open("out.txt", "w") as fo:
for line in fi:
line = line.strip()
if line[-1] == ",":
line = line[:-1]
fo.write(line + "\n")
Upvotes: 1