Change String to Float with (" ") in string [duplicate] - python

This question already has an answer here:
Reading CSV files in numpy where delimiter is ","
(1 answer)
Closed 4 years ago.
Ok, i have string like that in a file
"0.9986130595207214","16.923500061035156","16.477115631103516","245.2451171875","107.35090637207031","118.8438720703125","254.64633178710938","255.2373046875","264.1331481933594","28.91413116455078"
and i have multiple row.
how to change the data to float or number, i have problem because the item become ' "0.9986130595207214" '.
this code that i've write :
import numpy as np
data = np.loadtxt("data.csv",dtype=str,delimiter=',')
for y in data:
for x in y:
print(float(x))
and got error :
print(float(x)) ValueError: could not convert string to float:
'"0.9986130595207214"'
Thanks

From the error, you got:
x = '"0.9986130595207214"'
Thus, you first need to get rid of the brackets.
float(x.strip('"'))
Output:
0.9986130595207214

Related

Read string from html code and save to file [duplicate]

This question already has answers here:
Converting special characters to regular c#
(4 answers)
Closed 2 years ago.
How do I translate the bytes %C3%B8 in "testdoc%C3%B8%C3%B8%C3%B8.txt" to ø?
I tried the following which did not work:
var cont = response.Content.Headers.ContentDisposition?.FileName;
var bytes = Encoding.UTF8.GetBytes(cont);
var test = new string(bytes.Select(b => (char)b).ToArray());
var yourText = System.Text.Encoding.UTF8.GetString(bytes);
Don't bother with converting it to bytes at all. As has been noted in the comments, this is URL encoding, not UTF8.
Use HttpUtility in the System.Web namespace:
string input = "testdoc%C3%B8%C3%B8%C3%B8.txt";
string output = HttpUtility.UrlDecode(input);
Console.WriteLine(output); // testdocøøø.txt
Try it online

Formatting strings with integers

I'm trying to increment the video file names every time they get into my folder. I tried the + and the join() method but I can't seem to figure it out. I tried integers without quotation marks but the join method wont let me use an integer so I tried with quotation marks but now it won't increment
Here is my code
VideoNumber += "99"
folderLocation = ("C:/Users/someone/Documents", VideoNumber, ".mp4")
x = "/".join(folderLocation)
print(x)
You can format integers into a string using an f-string or the format() method on strings.
video_number += 99
video_path = f"C:/Users/someone/Documents/{video_number}.mp4"
print(video_path)
Just as an example of how to make your original code work, you could keep your number as an integer and then convert it to a string using str() (though note this has a bug because you will have an extra / between the number and .mp4).
VideoNumber += 99
folderLocation = ("C:/Users/someone/Documents", str(VideoNumber), ".mp4")
x = "/".join(folderLocation)
print(x)
You can cast the integer into string, so your code will be like this
folderLocation = ("C:/Users/someone/Documents", str(VideoNumber), ".mp4")

How to force Python not to use "..." in long strings? [duplicate]

This question already has answers here:
How do I print the full NumPy array, without truncation?
(22 answers)
Closed 1 year ago.
I used the following program:
def show(self):
output = str(self.inodes) + " " + str(self.hnodes) + " " + str(self.onodes) + " " + str(self.lr) + " " + str(self.wih) + " " + str(self.who)
return output
to get the stats of a neural network as a string, which i then want to save via:
networkSave = n.show()
datei = open("FILEPATH/FILENAME.txt", mode='w')
datei.write(networkSave)
datei.close()
in a txtfile. The code works good so far. The problem is though that in my design the array "self.wih" has over 70.000 entries and I get those dots in the said txt file where the array is only abbreviated depicted:
[[ 0.02742568 0.07564016 0.01795626 ... 0.01656147 -0.07529069
0.00203228]
[ 0.01877599 -0.07540431 -0.02055005 ... 0.03289611 -0.01307233
-0.01261936]
[-0.0029786 -0.05353505 -0.04538922 ... -0.004011 -0.03398194
-0.0058061 ]
Anyone has a clue how to force python to give the full array as string?
Cannot add comments due to reputation, but your question seems to be answered here: How to print the full NumPy array, without truncation?
Bottom line:
numpy.set_printoptions(threshold=sys.maxsize)

How do I make a text to binary? [duplicate]

This question already has answers here:
How to convert string to binary?
(9 answers)
Closed 2 years ago.
So I have been trying to make a text to binary converter but what I do is I convert every single letter and symbol to the binary equivalent but as you can imagine this takes long and i'm wondering if there is a shorter way to do this.
Text to binary convert :
name = "Name"
binary = ' '.join(format(ord(x), 'b') for x in name)
Binary to Text :
binary = '100001'
binary_values = binary.split()
ascii_string = ""
for binary_value in binary_values:
an_integer = int(binary_value, 2)
ascii_character = chr(an_integer)
ascii_string += ascii_character
print(ascii_string)
Here is my git repo
This programm convert text to binaries list :
a_string = "abc"
a_byte_array = bytearray(a_string, "utf8") #Create bytearray
byte_list = []
for byte in a_byte_array:
binary_representation = bin(byte) #Convert to binary
byte_list.append(binary_representation) #Add to list
print(byte_list)
There are plenty of binary modules on PyPI. You could just have the input go through one and print the result.

saving array of string and integers to a text file in python [duplicate]

This question already has answers here:
numpy save an array of different types to a text file
(2 answers)
Closed 5 years ago.
I have a python list
temp = [['abc.jpg', 1, 2, 3, 'xyz'], ['def.jpg', 4,5,6, 'xyz']]
To save it as an array, so I do this:
temp = np.vstack(temp)
Result:
print(temp)
temp = [['abc.jpg', '1', '2', '3', 'xyz'], ['def.jpg', '4','5','6', 'xyz']]
It is converting the integers to string. I dont want that to happen.
I want to save the result in a text file.
I tried the following:
np.savetxt("data.txt", temp)
But I get the following error:
TypeError: Mismatch between array dtype ('<U8') and format specifier ('%.18e %.18e %.18e %.18e %.18e %.18e')
try this(it saves every row separated by ";"):
np.savetxt("data.txt", temp, delimiter=" ", newline = "\n", fmt="%s")

Categories

Resources