Cannot load csv file using numpy loadtxt - python-3.x

I cannot load a csv file using the numpy loadtxt function. There must be something wrong with the file format or something else. I am using anocanda notebook on macbook.
OSError: Macintosh HD⁩\\Users⁩\\binhao⁩\\Downloads⁩\\Iris_data.csv not found.
np.loadtxt("Macintosh HD⁩\\Users⁩\\binhao⁩\\Downloads⁩\\Iris_data.csv")
I tried a solution I found on stackflow involved using:
f = open(u"Macintosh HD⁩\\Users⁩\\binhao⁩\\Downloads⁩\\Iris_data.csv")
f = open("Macintosh HD⁩\\Users⁩\\binhao⁩\\Downloads⁩\\Iris_data.csv")
Above don't work - No such file or directory error

Most of the time is due to some non-escaped charter, try to use raw string:
r"Macintosh HD⁩\\Users⁩\\binhao⁩\\Downloads⁩\\Iris_data.csv"

Related

pandas : read_csv not accepting relative path

I have python code in Jupyter notebook and accompanying data in the same folder. I will be bundling both the code and data into a zip file and submitting for evaluation. I am trying to read the data inside the Notebook using pandas.read_csv using a relative path and thats not working. the API doesnt seem to work with relative path. What is the correct way to handle this?
Update:
My findings so far seem to suggest that, I should be using os.chdir() to set the current working directory. But I wouldn't know where the zip file will get extracted. The code is supposed to be read-only..So I cannot expect the receiver to update the path as appropriate.
You could append the current working directory with the relative path to avoid problem as such:
import os
import pandas as pd
BASE_DIR = os.getcwd()
csv_path = "csvname.csv"
df = pd.read_csv(os.path.join(BASE_DIR, csv_path)
where csv_path is the relative path.
I think first of all you should make a unzip file then you can run.
You may use the below code to unzip file,
from zipfile import ZipFile
file_name = "folder_name.zip"
with ZipFile(file_name, 'r') as zip:
zip.extractall()
print("Done !")

How to write solution to a .sol file while using cbc in pyomo? How can I get the name of the .sol file after model is solved?

I am using pyomo on Jupyter Notebook. I have kept keepfiles = true in solve.I am able to get the location of .sol file where it is stored. How can I get the filename of the .sol file created for the current instance?
I have used following:
from pyomo.opt import SolverFactory
SolverFactory("cbc").options['solu']="solution_file.sol"
But this does not work in creating the desired solution file.
If you add the keepfiles=True option to your call to solve the temporary files that are used to pass the model to the solver and read in the results will not be deleted and the path to them will be printed on the screen. So I would create and call your solver using something like:
from pyomo.opt import SolverFactory
solver = SolverFactory("cbc")
solver.solve(model, keepfiles=True)

Getting error to read edf file using mne-python

I am trying to visualize the EEG data fie which is in .edf file format.For this purpose I am using MNE python.
here is my code
import mne
file = "/home/test.edf"
data = mne.io.read_raw_edf(file,preload=True)
Whenever I run this code below error massage is showing
ValueError: not enough values to unpack (expected 3, got 0)
I could not figure out where is my wrong.
It's not possible to use use the file by specifying the path, event if you add a "~", the directory won't be identified. Better you try to be in the exact directory and try reading the file, i.e go to your home directory and then try specifying the file.
import mne
file = "test.edf"
data = mne.io.read_raw_edf(file)

Camelot-py does not work in loops but works for an individual file

I am currently working on an automation project for a company, and one of the tasks require that I loop through a directory and convert all the pdf files into a CSV file. I am using the camelot-py library (which has been better than the others I have tried). When I apply the code below to a single file, it works just fine; however, I wish to make it loop through all pdf files in the directory. I get the following error with the code below:
"OSError: [Errno 22] Invalid argument"
import camelot
import csv
import pandas as pd
import os
directoryPath = r'Z:\testDirectory'
os.chdir(directoryPath)
print(os.listdir())
folderList = os.listdir(directoryPath)
for folders, sub_folders, file in os.walk(directoryPath):
for name in file:
if name.endswith(".pdf"):
filename = os.path.join(folders,name)
print(filename)
print(name)
tables = camelot.read_pdf(filename, flavor = 'stream', columns= ['72,73,150,327,442,520,566,606,683'])
tables = tables[0].df
print(tables[0].parsing_report)
tables.to_csv('foo2.csv')
I expect all files to be converted to '.csv' files but I get the error 'OSError: [Errno 22] Invalid argument'. My error appears to be from line 16.
I don’t know if you have the same problem, but in my case I made a really stupid mistake of not putting the files in the correct directory. I was getting the same error but once I found out the problem, script works within a regular for loop.
Instead of the to methods, I am using the bulk export to export the results in sql, but that should not be a problem.

FileNotFoundError file does not exist, but it does exist when using df = pd.read_csv. Python 3, Spyder IDE

I'm attempting to compile some stock data that I have downloaded and stored locally. the locally stored data is in one folder, and all contain non-corrupted csv files. This is the code I'm using to read the folder containing all of my saved csv files:
df = pd.read_csv('stock_dfs/{}.csv'.format(ticker.replace('.'and'-'and',','')))
When i run my full program, the ticker BRK.B always shows up as FileNotFoundError:
FileNotFoundError: File b'stock_dfs/BRK-B.csv' does not exist
The error is interesting because in my "stock_dfs" folder, the ticker is written as BRK.B and I thought the pandas reader couldn't identify a ticker with a '.'.
Attempting to fix this error I added the code below because I thought it might have to do with the '.', but then it changed to a '-', then to a ',' and when i finally covered all of those it still gives the error message with a '-': (the 'change' i'm referring to is in the punctuation between 'BRK' and 'B' when the error is printed.)
ticker.replace('.'and'-'and',','')))
but I still receive the FileNotFoundError:
FileNotFoundError: File b'stock_dfs/BRK-B.csv' does not exist
I looked at other questions like CSV file doesn't exist - pandas dataframe, but even then when I provided a full path to the folder, and even to the BRK.B csv file, it produced the same error as above. The tip of specifying the path to the folder did not help, and I for the life of me cannot figure out why it keeps changing the punctuation to one of the ticker's in the error, as well as not find a csv file that clearly is there.
Any help is greatly appreciated.
here is an image of the BRK.B file in the folder, as well as opened up in excel
On windows, the folder separator is \ and not /.
Try the following:
df = pd.read_csv('stock_dfs\{}.csv'.format(ticker))
If you want system independent separator, use os.sep after import os, or just from os import sep.
If you want to replace a lot of symbols into nothing, try:
symbols = '-,.'
for c in symbols:
if c in ticker:
ticker.replace(c, '')
There is clearly a discrepancy between the ticker names and file names, but without knowing how you named your files compared to ticker names from Google, it's hard to guess the specific error.
To find the name, you can try to find it in the folder:
from os import listdir, getcwd
from os.path import isfile, join
print([f for f in listdir(mypath) if isfile(join(mypath, f)) and f.upper().startswith('BRK')])
That will list all files in the mypath folder that starts with BRK.
And, as a last not:
The error is interesting because in my "stock_dfs" folder, the ticker is written as BRK.B and I thought the pandas reader couldn't identify a ticker with a '.'.
There is nothing wrong with having '.' (dot) in the file name, i.e. you could perfectly have a file called some.file.with.long.name.txt.csv
I have found the issue. When i get the ticker symbols from Wikipedia and compile them into a csv file, all tickers that use '.' are supplemented with a ',' then '-' when printed.

Resources