Inexplicable value error due to encoding of file in Python - python-3.x

I have a file with saved modelling data like this
1
0
1
Trying to iterate over the file using readlines, which correctly reads the line as zero or one.
Error happens when I cast the string into 1 or 0 Value error my code is this
st1=myfile.readline()
try:
line=float(st1.strip(), base=10)
if line==0: zeros+=1
if line==1 or line==0: ones+=1
except ValueError
I am getting value error in each line. I have no clue what is causing this. I do not want to load whole file into a variable as it is quite large. I tried changing 1 to 100 still error.Though a trivial error, I am unable to fix it.I Searched online forums, stack overflow no help. I don't want Numpy, please don't give some solution using Array operation on NumPy. Can you please help?

Related

How to get saturation in a unsaturated model MODFLOW-USG?

I'm a beginner in FloPy and I'm working with a unsatured model in MODFLOW-USG (using Richards equation), and I have the output files (.hds, .ddn, .cbb). I need to export saturation of unsatured cells (a parameter between 0 and 1) but I can't find how to do it. I've seen in some papers the parameter 'water content', 'water saturation', 'saturation' but I don't know how to get it...
I've been trying to get Saturation from output file .DDN postprocessing it in FloPy with the code:
flopy.utils.binaryfile.HeadFile(f"DRPA_TR_oc_.ddn",text="SATURATIONU", precision="auto")
and
flopy.utils.binaryfile.HeadFile(f"DRPA_TR_oc_.ddn",text="SATURATION", precision="auto")
but after ~1 hour computing following appears:
File "C:\Users\User1\Anaconda3\lib\site-packages\flopy\utils\binaryfile.py", line 369, in _get_header
return header[0]
IndexError: index 0 is out of bounds for axis 0 with size 0
when I ran CellBudgetFile with .cbb or HeadUFile with .hds it took few minutes. So, I don't understand very well where is the error and how to fix it, and why to compute .ddn file take a lot of time.
I hope you can help me to fix the error or another way to get saturation (maybe from .hds, or whatever) :)
I remain attentive to your comments!
Thanks in advance :)

Why list index out of range?

count = [0]*1001
count[1000] = 0
Getting this issue on LeetCode, and ran into this. I would expect the above to create 1001 elements, or 0 to 1000 valid positions. But getting a list index error instead.
i've ran your code in the command line with python 3.8.0 and no IndexError was flagged? Is this the exact code your getting the error from or is a similar example to the code you're using?

Python Machine Learning Regression P.1 pandas error

Pycharm screenshot of error message with code above
I don't know what to do about the error. I don't know what it means
also there should print(data.head()) between line 8 & 9.
The error is happening in your data = read.csv() line. Check your working directory, or point explicitly to the file location, using something like:
data = read.csv("C:/Users/user/filename.csv")

error uploading csv file on cloud jupyter notebook

I have set up a google cloud account
I want to perform my deep learning much more faster on a jupyter notebook, but
I cannot find a way to read my csv file
I downloaded it with wget from my github account and afterwards I tried
dataset = pd.read_csv('/home/user/.jupyter/SIEMENSTRAIN.csv')
but I get the following error
pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 fields in line 3, saw 12
Why? When I read it on my laptop using my jupyter notebooks, everything runs well
Any suggestions?
I tried the recommended solutions for this error and I got the next warning
/home/user/anaconda3/lib/python3.5/site-packages/ipykernel/main.py:1: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support regex separators; you can avoid this warning by specifying engine='python'.
if name == 'main':
When I ran dataset.head() this is what appeared
Any help please?
There are a number of possibilities that could be causing the problem... I would first always make sure that Pandas (pd)'s version is updated and compatible.
The more likely cause is that the CSV itself is not right, so pd.read_csv() is not able to work correctly (thus a Parse Error). This may have something to do with the headers, though I'm not sure what your original CSV file looks like. It's worth playing around with read_csv, for example:
df = pandas.read_csv(fileName, sep='delimiter', header=None)
This tampers with 2 things - the delimiter, and if pd is reading a header from CSV or not.
I go through some pd.read_csv() stuff in my book about Stock Prediction (another cool Machine Learning problem) and Deep Learning, feel free to check it out.
Good Luck!
I tried what you proposed and this is what I got
So, any suggestions?
I suppose that the path is ok, but it just won't be read properly, or am I wrong?

Name error when calling defined function in Jupyter

I am following a tutorial over at https://blog.patricktriest.com/analyzing-cryptocurrencies-python/ and I've got a bit stuck. I am tyring to define, then immediately call, a function.
My code is as follows:
def merge_dfs_on_column(dataframes, labels, col):
'''merge a single column of each dataframe on to a new combined dataframe'''
series_dict={}
for index in range(len(dataframes)):
series_dict[labels[index]]=dataframes[index][col]
return pd.DataFrame(series_dict)
# Merge the BTC price dataseries into a single dataframe
btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')
I can clearly see that I have defined the merge_dfs_on_column fucntion and I think the syntax is correct, however, when I call the function on the last line, I get the following error:
NameError Traceback (most recent call last)
<ipython-input-22-a113142205e3> in <module>()
1 # Merge the BTC price dataseries into a single dataframe
----> 2 btc_usd_datasets= merge_dfs_on_column(list(exchange_data.values()),list(exchange_data.keys()),'Weighted Price')
NameError: name 'merge_dfs_on_column' is not defined
I have Googled for answers and carefully checked the syntax, but I can't see why that function isn't recognised when called.
Your function definition isn't getting executed by the Python interpreter before you call the function.
Double check what is getting executed and when. In Jupyter it's possible to run code out of input-order, which seems to be what you are accidentally doing. (perhaps try 'Run All')
Well, if you're defining yourself,
Then you probably have copy and pasted it directly from somewhere on the web and it might have characters that you are probably not able to see.
Just define that function by typing it and use pass and comment out other code and see if it is working or not.
"run all" does not work.
Shutting down the kernel and restarting does not help either.
If I write:
def whatever(a):
return a*2
whatever("hallo")
in the next cell, this works.
I have also experienced this kind of problem frequently in jupyter notebook
But after replacing %% with %%time the error resolved. I didn't know why?
So,after some browsing i get that this is not jupyter notenook issue,it is ipython issueand here is the issue and also this problem is answered in this stackoverflow question

Resources