How to write a function output into .txt file? [closed] - function

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have to write a function that produces random(parameter) integers i have to write random output of this funtion into .txt file. Also i have to find max and min integers from this function (write them into .txt too)
I cant find any tutorial about this subject. Want to use "a+" as writer.

Firstly, please have a look online for tutorials on how to write to a file. There are loads of free resources online.
You have already done a lot of the work and only needed few more lines
import random
def createRandomNumberFile(min, max, count, outputFileName):
outputFile = open(outputFileName, 'a+')
for item in range(0, count):
randomNumber = random.randint(min,max)
outputFile.write(randomNumber)
outputFile.close
Explanation - As you have assigned the file using the 'a+' mode (appending mode) to a variable (outputFile) you can simply use this variable to write to the file and then later close file (closing the file isn't always necessary but is good practice).
Hope that helps

Related

Get a certain string before the specified string and after the second specified string [BATCH] [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 months ago.
Improve this question
I'm making a Batch script to get the actual stock of an product on a page,
The JSON is :
... ,"src":"https://www.datocms-assets.com/34299/1615563408-h510-elite-white-black-fan-bracket-out.png?ar64=MTox&auto=format&fit=crop&h=375&w=375","width":375,"height":375,"alt":null,"title":null}}],"aliasImage":null,"price":179.99,"stock":0,"id":260,"shopify_variants_id":39412773781641,"shopify_parent_id":6596349198473,"isValid":true,"isLowStock":true,"isOutOfStock":true,"discounted_price":124.99,"isOnSale":true,"isComingSoon":false,"isNewItem":false,"showSaleTag":false,"isPromo":true,"showCountdownTimer":false,"countdownEndDate":null,"showPercentOff":true,"percentOff":30,"showInventoryCount":false,"showAmountOff":false,"amountOff":0,"isBestSeller":false} ...
I need to get the stock, the and the dismounted price, if someone would be able to help me make this, i would appreciate it.
I precise that they are multiples : "discounted_price" and "price" on the json, it contains all the nzxt product
i've thinked about getting the string before : "id":260 but found nothing working correctly.
i dont need exactly this idea, but i need: something that work! thanks you ^^
This is not a code-writting service. You should write your own code and we give help with it.
A Batch file is not the right tool to process JSON data.
We need an actual data in order to do tests on it.
Anyway, this is a small Batch code that may help:
#echo off
setlocal
set json={"src":"https://www.datocms-assets.com/34299/1615563408-h510-elite-white-black-fan-bracket-out.png?ar64=MTox&auto=format&fit=crop&h=375&w=375","width":375,"height":375,"alt":null,"title":null}}],"aliasImage":null,"price":179.99,"stock":0,"id":260,"shopify_variants_id":39412773781641,"shopify_parent_id":6596349198473,"isValid":true,"isLowStock":true,"isOutOfStock":true,"discounted_price":124.99,"isOnSale":true,"isComingSoon":false,"isNewItem":false,"showSaleTag":false,"isPromo":true,"showCountdownTimer":false,"countdownEndDate":null,"showPercentOff":true,"percentOff":30,"showInventoryCount":false,"showAmountOff":false,"amountOff":0,"isBestSeller":false}
rem Remove from beginning up to "width"
set "json=%json:*width="width%"
rem Change quote-colon by equal-sign
set "json=%json:":==%"
rem Separate comma-delimited parts and assign them to individual variables
set %json:,=" & set %
echo stock=%stock%
echo discounted_price=%discounted_price%
The output of this code is:
stock=0
discounted_price=124.99
Please, do not post a comment indicating that this code does not work with your actual data! :( Instead, remove the #echo off line, run the program, comprehend how it works and insert the needed modifications until the program correctly work...

Exception of 50000 characters at 'SpreadsheetApp.flush()' in Google Sheets? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm trying to import a list from my Gmail into a spreadsheet.
It has worked flawlessly several times, but I decided to do some cosmetic changes and now I get a baffling error:
var l = artikelen.length; //l = 40 in this case
var importSh = SpreadsheetApp.getActive().getSheetByName("import");
var range = importSh.getRange(2,1,l,1);
range.setBackground("#ab6e6e");
SpreadsheetApp.flush(); //throws error 'Input exceeds the limit of 50000 characters in a cel'
What is that? I'm not trying to put anything in a cell?? I'm just ordering to pick a range and set a colour AFAIK. Which in fact is not executed either...
What am I overlooking here?
Any suggestion will be welcome.
The culprit may be elsewhere in your code. Chances are that there is an attempt to put more than 50,000 characters in some cell.
Spreadsheet writes are lazy, and the error may only get shown when the script terminates, or at flush(), as the case seems to be here. To debug, add a flush() after every Range.setValues() and Range.setValue() call. Use console.log() to find what the values you are writing look like.

What does "Save Value into the Parameters of a Method" Mean (Classes in C++) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I've got an assignment that is due tomorrow..
I don't want the entire solution, there's just a part of the program that i dont understand.
It's highlighted in the image below:
(I dont know what "save the values into the parameters of a method" means)
As you can see the type of the parameters is double& which means you are storing the values within the arguments you are sending. e.g, if you have 3 doubles a,b & c, when you call getAll(a,b,c) the result should be stored in them.
You can find more detailed explanation about &(reference) operator in
What does '&' do in a C++ declaration? .

Erlang store file contents in a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
code
read_CNF_File(FileName)->
case file:read_file(FileName) of
{ok, Data} -> print(binary:split(Data, [<<"\n">>], [global]));
{error, Reason} -> Reason end.
print([]) -> ok;
print([L|List]) ->
L,
print(List).
[Pic Related] How do I store contents from a file into a list (ideally a list of every line), if I try to io:fwrite As in read_CNF_File it seems to store it the way I want, however once I try to call print with that it just passes As as an empty list, thanks.
It is your print function that does nothing.
If you pass it à non empty list, it removes the first element (A in your code), the statement A, does nothing, and then recursively call itself with the tail of the list, until it is empty a' finally returns OK.

Read info from hyperlink located remotely (know that by hyperlink it will be csv file, but can't find general approach) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am using Python 3.4. I have tried find solution in the web, but still didn't.
I have a link to csv file (data set).
Is there a way to fetch information from this link without duplicating it in the local directory?(e.g., I don't have enough space on the disk)
I would like to continue working with data that will be in the RAM.(e.g., I am planning to find out how many datarows and have to do some Data Mining and Filtering stuff, currently not important what it will be)
Try the following:
import requests
r = requests.get('http://127.0.0.1/some_path/small.csv')
print len(r.content.split('\n')) -1
Result:
10
for small.csv file as follows:
1lpcfgokakmgnkcojhhkbfbldkacnbeo,6B5108
pjkljhe2ncpnkpknbcohdijeoejaedia,678425
apdfllc5aahabafndbhieahigkjlhalf,651374
aohghmighlieiainnegkcijnfilokake,591116
coobgpohoikkiipiblmjeljniedjpjpf,587200
dmgjnkhnkblpmfjpdakehnaikgdjllic,540979
felcaaldnbdncclmgdcncolpebgiejap,480535
aapocclcgogkmnckokdopfmhonfmgoek,480441
pdehmppfilefbolgganhfihpbmjlgebh,273609
nafaimnnclfjfedmmabolbppcngeolgf,105979
Edit: (As suggested by MHawke)
import requests
line_cnt=0
r = requests.get('http://127.0.0.1/some_path/small.csv',stream=True)
for i in r.iter_lines():
if i.strip():
line_cnt +=1
print (line_cnt)
This version does not count blank lines and should be more efficient for a large file because it uses iter_lines
iter_lines(chunk_size=512, decode_unicode=None, delimiter=None)
Iterates over the response data, one line at a time. When
stream=True is set on the request, this avoids reading the content at
once into memory for large responses.
(Note: not re-entrant safe)