python系列 文件操作的代碼
核心代碼
import numpy as np
import os,sys
#獲取當(dāng)前文件夾,并根據(jù)文件名
def path(fileName):
p=sys.path[0]+'\\'+fileName
return p
#讀文件
def readFile(fileName):
f=open(path(fileName))
str=f.read()
f.close()
return str
#寫文件
def writeFile(fileName,str):
f=open(path(fileName),'w')
f.write(str)
f.close()
def str1():
str=','.join('我在中國大地上驕傲地生長著!')
return str
def str2():
return str(np.random.randint(-49,50,[3,3,3]))
#實驗1
def test_1():
fileName='中國大地.txt'
writeFile(fileName,str1())
list=readFile(fileName).split(',')
print(list)
#實驗2
def test_2():
writeFile('str1',str1())
writeFile('str2',str2())
str_1=readFile('str1')
str_2=readFile('str2')
print(str_1)
print(str_2)
test_2()
下面是一些
打開和關(guān)閉示例:

讀取


寫入


randint(low[,high,shape]) 根據(jù)shape創(chuàng)建隨機整數(shù)或整數(shù)數(shù)組,范圍是[low, high)
numpy.random.randint的詳細(xì)用法
函數(shù)的作用是,返回一個隨機整型數(shù),范圍從低(包括)到高(不包括),即[low, high)。如果沒有寫參數(shù)high的值,則返回[0,low)的值。
numpy.random.randint(low, high=None, size=None, dtype='l')
參數(shù)如下:
| 參數(shù) | 描述 |
|---|---|
| low: int | 生成的數(shù)值最低要大于等于low。 (hign = None時,生成的數(shù)值要在[0, low)區(qū)間內(nèi)) |
| high: int (可選) | 如果使用這個值,則生成的數(shù)值在[low, high)區(qū)間。 |
| size: int or tuple of ints(可選) | 輸出隨機數(shù)的尺寸,比如size=(m * n* k)則輸出同規(guī)模即m * n* k個隨機數(shù)。默認(rèn)是None的,僅僅返回滿足要求的單一隨機數(shù)。 |
| dtype: dtype(可選): | 想要輸出的格式。如int64、int等等 |
輸出:
返回一個隨機數(shù)或隨機數(shù)數(shù)組
例子
>>> np.random.randint(2, size=10)
array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
>>> np.random.randint(1, size=10)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
[3, 2, 2, 0]])
>>>np.random.randint(2, high=10, size=(2,3))
array([[6, 8, 7],
[2, 5, 2]])
好了這篇文章先介紹到這,后續(xù)腳本之家小編會為大家分享更多的資料。
相關(guān)文章
python3發(fā)送request請求及查看返回結(jié)果實例
這篇文章主要介紹了python3發(fā)送request請求及查看返回結(jié)果實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
python使用tomorrow實現(xiàn)多線程的例子
今天小編就為大家分享一篇python使用tomorrow實現(xiàn)多線程的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
20行Python代碼實現(xiàn)一款永久免費PDF編輯工具的實現(xiàn)
這篇文章主要介紹了20行Python代碼實現(xiàn)一款永久免費PDF編輯工具的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

