Scipy.io not loading data - python

I'm trying to use scipy.io to load the contents of a .mat file to a python dictionary. This seems to be working except in regard for the data in the table "data". This seems to be working regardless of the file I'm loading. Is there a way to fix this, or a workaround that I can use? I've looked into mat4py, mat73, mat2py and I can't seem to get any of them working. The code definitely finds the correct file.
import scipy as sp
import os
Dir=r"Directory"
os.chdir(Dir)
mat_contents = scipy.io.loadmat("file")
mat_contents['data']
This returns the following error "KeyError: 'data'"
In addition,
mat_contents.keys()
Gives the following error
dict_keys(['header', 'version', 'globals', 'None', 'function_workspace'])

Related

footballdata package - FileExistsError: [WinError 183] Cannot create a file when that file already exists

I know this particular question has been asked loads of times but I have searched through and tried multiple approaches to solve this all with the same issue still existing so unfortunately have to ask for help because I cannot solve myself.
I have the following package installed: https://github.com/skagr/footballdata
When trying to read_games I get the dreadful error message. After reading through some of the package it looks as though it should overwrite the data if it already exists but I think this is more of a windows error.
Any help will be appreciated.
I have tried creating a parent structure in the folders but the same error exists. Also tried using Pathlib to say if folder exists=OK but still doesnt solve the error.
edit: Code added.
import numpy as np
import pandas as pd
import requests
import unidecode
import footballdata as foo
import pathlib
pathlib.Path('C:\\Users\\username\\.spyder-py3\\data').mkdir(parents=True, exist_ok=True)
EPL = foo.MatchHistory('ENG-Premier League', [22]).read_games()
print(EPL.head())
As you can see there isnt much to it but getting the error stops anything getting past this point.
thanks

Loading .dat file in python

I made a simple code that loads in data called 'original.dat' from a folder named 'data' in my computer. The code was working great and i was able to view my spectra graph. This morning I ran the code again, but it completely crashed giving the error " OSError: data/original.dat not found." Even though nothing changed. The data is infact still in the folder named 'data' and there isn't any spelling mistakes. Can anyone please help understand why its now giving me this error? The code was working perfectly the day before.
here is the code I used :
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
OPUSimaginary = np.loadtxt('data/original.dat', delimiter = ',')
Data file position, Error:cant find the file,Error: suggested code to find file
Few things that you can do to avoid file not found type of issues:
Add a check if the file exists before trying to load it.
Example:
import os
os.path.isfile(fname) # Returns True is file found
Make sure the file permissions are correct and the file can be read.
sudo chmod -R a+rwx <file-name>

CSV file not found even though it exists (FileNotFound [Errno 2])

I was trying to read a dataset but I'm getting this weird error:
FileNotFoundError: [Errno 2] File b'Position_Salaries.csv' does not exist: b'Position_Salaries.csv'
Even though the file does exist and its on the same directory of my code (As you can see in this pic)
The weird thing is, I've run that code many times without getting any kind of error, but suddenly (without changing anything)is not working. I literally just opened Spyder, try to run the code, but nope, that error appeared. Any ideas why is this happening and how can I solve it?
Part of the code I'm trying to run:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values
There is a tool tip that is obscuring your picture but from the information provided I would guess that your working directory is not set to your project directory. Try running the code from the terminal.
Sometimes it happens due to the changes in the format of your excel file. Make sure that the excel file is saved as CVS.

Unseekable HTTP Response

I am accessing some .mat files hosted on a SimpleHTTP server and trying to load them using Python's loadmat:
from scipy.io import loadmat
import requests
r = requests.get(requestURL,stream=True)
print loadmat(r.raw)
However, I am getting an error
io.UnsupportedOperation: seek
After looking around it seems that r.raw is a file object which is not seekable, and therefore I can't use loadmat on it. I tried the solution here (modified slightly to work with Python 2.7), but maybe I did something wrong because when I try
seekfile = SeekableHTTPFile(requestURL,debug=True)
print 'closed', seekfile.closed
It tells me that the file is closed, and therefore I can't use loadmat on that either. I can provide the code for my modified version if that would be helpful, but I was hoping that there is some better solution to my problem here.
I can't copy/download the .mat file because I don't have write permission on the server where my script is hosted. So I'm looking for a way to get a seekable file object that loadmat can use.
Edit: attempt using StringIO
import StringIO
stringfile = StringIO.StringIO()
stringfile.write(repr(r.raw.read())) # repr needed because of null characters
loaded = loadmat(stringfile)
stringfile.close()
r.raw.close()
results in:
ValueError: Unknown mat file type, version 48, 92

Error: Line magic function

I'm trying to read a file using python and I keep getting this error
ERROR: Line magic function `%user_vars` not found.
My code is very basic just
names = read_csv('Combined data.csv')
names.head()
I get this for anytime I try to read or open a file. I tried using this thread for help.
ERROR: Line magic function `%matplotlib` not found
I'm using enthought canopy and I have IPython version 2.4.1. I made sure to update using the IPython installation page for help. I'm not sure what's wrong because it should be very simple to open/read files. I even get this error for opening text files.
EDIT:
I imported traceback and used
print(traceback.format_exc())
But all I get is none printed. I'm not sure what that means.
Looks like you are using Pandas. Try the following (assuming your csv file is in the same path as the your script lib) and insert it one line at a time if you are using the IPython Shell:
import pandas as pd
names = pd.read_csv('Combined data.csv')
names.head()

Categories

Resources