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)
Related
This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 1 year ago.
I have one problem with print in Python, I am starting to learn Python, but when I want to print variables in print function, it looks like that after one variable it adds newline to the outcome:
print(game_name + " | " + rating)
I am making a game database with my own ratings on the games but if it prints the game and the rating there is like one empty line belpw the game_name and rating is written, how can I delete that empty newline? I am very new to this, so please don't be mean...
Welcome to Stack Overflow! The most likely culprit is that there is a newline at the end of the game_name variable. The easy fix for this is to strip it off like this:
print(game_name.strip() + " | " + rating)
Say we had two variables like this.
game_name = 'hello\n'
rating = 'there'
game_name has the newline. To get rid of that use strip().
print(game_name.strip() + " | " + rating)
output
hello | there
If you want to remove the line break after printing, you can define the optional end parameter to the print statement.
print('Hello World', end='') # No new line
If your variable has a new line added to it, and you want to remove it, use the .strip() method on the variable.
print('Hello World\n'.strip()) # No empty line
For your code, you could run it:
print(game_name + " | " + rating, end='')
Or
print(game_name + " | " + rating.strip())
If the error is that a new line is printed after game_name, you'll want to call the strip method on that instead.
print(game_name.strip() + " | " + rating)
rating or game_name most likely have a new line after the specified string.
You can fix it by doing this:
game_name = game_name.strip('\n')
rating = rating.strip('\n')
print(game_name + " | " + rating)
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
This question already has answers here:
How can I use `return` to get back multiple values from a loop? Can I put them in a list?
(2 answers)
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 5 years ago.
I want to use a function as a String, i have this function:
def getCoins(user): ##Get the Coins from a user.##
try:
with open(pathToUser + user + ".txt") as f:
for line in f:
print(user + " has " + line +" coins!")
except:
print("Error!")
Now it just print the coins, but i want to use it in other codes like this:
client.send_msg(m.text.split(' ')[2] + "has" + coinapi.getCoins('User') + "coins")
How do it do that? So that i can use it like a string, the message in Twitchchat should be:
"USERXYZ has 100 coins"
Return a string
def getCoins(user): ##Get the Coins from a user.##
try:
with open(pathToUser + user + ".txt") as f:
return '\n'.join(user+" has "+line +" coins!" for line in f)
except:
return "Error!"
Also you should use format strings (assuming python 3.6)
def getCoins(user): ##Get the Coins from a user.##
try:
with open(f"{pathToUser}{user}.txt") as f:
return '\n'.join(f"{user} has {line} {coins}!" for line in f)
except:
return "Error!"
You should be able to just return the string you want in the output.
It looks like your code is iterating over f and printing the number of coins on each line, so you may need to return a list or generator of the coins amount, but the key answer to your question is just to return a string or something that can be turned into a string
This question already has answers here:
String comparison doesn't seem to work for lines read from a file
(2 answers)
Closed 5 years ago.
I'm trying to write a small Python script to generate CentOS7 kickstart configs. I have a skeleton config file and based on some user inputs, the script will pop out a custom cfg file by inserting the customized blocks into the skeleton. However, the string comparison is not working for some reason.
#!/usr/bin/python
type = raw_input("Static OR DHCP: ")
gateway = raw_input("Gateway IP: ")
nameserver = raw_input("DNS Server: ")
hostname = raw_input("Hostname: ")
ipaddr = raw_input("IP Address: ")
skeleton = open('ks_skeleton.cfg', 'r')
config = open(hostname + '.cfg', 'w')
for line in skeleton:
if line == "$NETWORK":
print("Interting Network values...");
config.write("network --bootproto=" + type + " --device=ens192 --gateway=" + gateway + " --ip=" + ipaddr + " --nameserver=" + nameserver + " --netmask=255.255.255.0 --ipv6=auto --activate\n");
config.write("network --hostname=" + hostname + "\n");
else:
config.write(line);
The lines that you read from skeleton have new lines at the end, so the exact string comparison are probably not going to work. If you do line = line.strip() as the first line of your loop it will remove whitespace from before and after any text on the line, and might get you closer to what you want.
This question already has answers here:
How can I concatenate str and int objects?
(1 answer)
String formatting: % vs. .format vs. f-string literal
(16 answers)
Closed 8 years ago.
I want the results to be in the format Date, Question, Name and Score
file = open("results.csv", "a")
file.write('Date, Question, Name, Score\n' + date + ',' question + ',' + name + ',' + score + '\n')
file.close()
When I run this code i keep getting the error: TypeError: Can't convert 'int' object to str implicitly
You have to cast to any ints to string string before you can concat it to another and write to file.
str(score) # <-
file.write('Date, Question, Name, Score\n' + date + ',' question + ',' + name + ',' + str(score) + '\n')
Or use str.format:
with open("results.csv", "a") as f: # with closes your files automatically
f.write('Date, Question, Name, Score\n {}, {}, {}, {}'.format(date, question, name ,score))
You may also find the csv module useful
Then convert it explicitly to str:
file.write('Date, Question, Name, Score\n' + str(date) + ',' question + ',' + name + ',' + str(score) + '\n')