當前位置:首頁 » 網路資訊 » 怎樣用隨機流讀入一行一行的數據
擴展閱讀
南充迅達可以做網站嗎 2025-07-05 15:46:27

怎樣用隨機流讀入一行一行的數據

發布時間: 2022-11-27 08:01:35

❶ 如何從excel中隨機提取整行數據

先給數據做隨機編號。可用隨機函數:
1、插入輔助列(B列吧),在B1輸入=INT(RAND()*5000+1)回車並向下填充(假設有5000行)。
2、選B列——復制——原地不動——右鍵——選擇性粘貼——數值——確定。
3、選數據區域——菜單欄——數據——排序——主要關鍵字:列B——升序(或降序均行)——確定。
4、這樣就把各行給隨機打亂了。取前200行就是隨機提取的行。
5、如果怕把原來各行的順序打亂,B列排序前先插入一列(C列)——前兩格輸入1、2——選1、2兩格向下填充形成序列號——B列排序並提取後,選C列排序就可以恢復原狀了。

❷ python如何隨機讀取一行

#!/usr/bin/envpython
#coding:utf-8

defgetfilelines(filename,eol=' ',buffsize=4096):
"""計算給定文件有多少行"""
withopen(filename,'rb')ashandle:
linenum=0
buffer=handle.read(buffsize)
whilebuffer:
linenum+=buffer.count(eol)
buffer=handle.read(buffsize)
returnlinenum


defreadtline(filename,lineno,eol=" ",buffsize=4096):
"""讀取文件的指定行"""
withopen(filename,'rb')ashandle:
readedlines=0
buffer=handle.read(buffsize)
whilebuffer:
thisblock=buffer.count(eol)
ifreadedlines<lineno<readedlines+thisblock:
#inthisblock:findthelinecontent,andreturnit
returnbuffer.split(eol)[lineno-readedlines-1]
eliflineno==readedlines+thisblock:
#needcontinuereadlinerestpart
part0=buffer.split(eol)[-1]
buffer=handle.read(buffsize)
part1=buffer.split(eol)[0]
returnpart0+part1
readedlines+=thisblock
buffer=handle.read(buffsize)
else:
raiseIndexError


defgetrandomline(filename):
"""讀取文件的任意一行"""
importrandom
returnreadtline(
filename,
random.randint(0,getfilelines(filename)),
)


if__name__=="__main__":
importsys
importos
iflen(sys.argv)==1:
printgetrandomline("/home/tim/documents/users.csv")
else:
forfinfilter(os.path.isfile,sys.argv[1:]):
printgetrandomline(f)

對於超大文件建議用逐行或分塊的方式處理;逐行處理可能慢一些,但編碼更簡單清晰一點;上面給出的是按分塊方式處理的。

❸ c++如何讀取txt的數據(一行一行的讀,從第一行讀到最後一行)要詳細代碼

C++讀取txt文本數據並一行一行的讀方法如下:

1、使用C++提供輸入輸出流的getline函數可以實現整行讀取;

完整代碼如下:

#include<iostream>
#include<fstream>
usingnamespacestd;

intmain(){

//定義輸入文件流類對象infile
ifstreaminfile("test.txt",ios::in);

if(!infile){//判斷文件是否存在
cerr<<"openerror."<<endl;
exit(1);//退出程序
}

charstr[255];//定義字元數組用來接受讀取一行的數據

while(infile)
{
infile.getline(str,255);//getline函數可以讀取整行並保存在str數組里
cout<<str<<endl;
}

return1;
}

❹ c#如何隨機讀取文本一行,並從隨機讀取的那一行開始繼續讀取下一行,直到結束

List<string>list = new List<string>();

using (StreamReader reader= new StreamReader("test.txt"))
{
string line = reader.ReadLine();
while(line!="" && line != null)
{
list.Add(line);
line = reader.ReadLine();

}

}
Random r = new Random();
int index = r.Next(list.Count);
using (StreamWriter reader = new StreamWriter("Newtest.txt"))
{
for(int i = index ; i < list.Count ; i++)

{
reader.WriteLine(List[i]);

}

}

❺ excel 隨機提取 一行

隨機提取一行數據不是那麼簡單的
最簡單使用兩個函數,比如樣本在sheet1的第一行到第20000行
可以在sheet2的A1輸入=int(rand()*20000+1)
然後在A2單元格輸入
=index(sheet1!a:a,$a1)
公式往右拉復制

❻ java用隨機流讀取文本文件輸出來只有一行,怎麼讓它讀取文本裡面的換行啊

public static String Read(String FileName) {
String s1 = null;
try {
FileReader reader = new FileReader(FileName);
BufferedReader br = new BufferedReader(reader);

while ((s1 = br.readLine()) != null) {
s1 = br.readLine();
br.close();
reader.close();
return s1;
}
br.close();
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
FileClass.WirteFile("文件沒有找到", "2");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
FileClass.WirteFile("IOException異常", "2");
e.printStackTrace();
}
return null;

}

❼ java io 位元組流如何讀取一行數據,我要一行一行的讀取數據,因為數據都是按位算的,我要用位元組流讀取一行,

位元組是不存在行不行這個概念的,不過你可以每次讀取固定長度
字元流可以一行一行的讀取數據

❽ 如何在java中實現讀取一個txt文檔中的隨機一行

在代碼里寫入一個輸入輸出流即可。
具體實現如下:
BufferedReader bf= new BufferedReader(new FileReader("file"));
註:其中file替換為文件路徑;
bf.readLine();
註:即可實現一行一行讀取txt文檔。

❾ PHP如何隨機讀取txt文本內容中的隨機一行並顯示出來

使用file_content,然後裡面參數用個隨機值,這樣就隨機取了

❿ C語言里 一行一行從文件里讀入數據,怎麼做

關鍵技巧: 每行數據個數隨機,可用 c = fgetc(fin); ungetc(c,fin); 檢查是否讀到 換行符或文件結束符。
例如:
a.txt
1 2 3 45 56
6 7
8 9 10
至於記錄每行數據個數,存放到數組,等等,可以自己補充完善。
程序:
#include <stdio.h>
int main( )
{
FILE *fin;
int a,c;
fin=fopen("a.txt","rw");
while(1){
c = fgetc(fin);
if (c==EOF) break;
if (c=='\n') {printf("\n==========\n"); continue;};
ungetc(c,fin);
fscanf(fin,"%d",&a); printf("%d ",a);
}
fclose(fin);
return 0;
}