我有一小块ruby,它创建一个包含tsv内容的文件,包含2列,日期和随机数.
#!/usr/bin/ruby
require 'date'
require 'set'
startDate=Date.new(2014,11,1)
endDate=Date.new(2015,9,1)
dates=File.new("/PATH_TO_FILE/dates_randoms.tsv","w+")
rands=Set.new
while startDate <= endDate do
random=rand(1000)
while rands.add?(random).nil? do
random=rand(1000)
end
dates.puts("#{startDate.to_s.gsub("-","")} #{random}")
startDate=startDate+1
end
Run Code Online (Sandbox Code Playgroud)
然后,从另一个程序,我读取此文件并从随机数创建一个文件:
dates_file=File.new(DATES_FILE_PATH,"r")
dates_file.each_line do |line|
parts=line.split("\t")
random=parts.at(1)
table=File.new("#{TMP_DIR}#{random}.tsv","w")
end
Run Code Online (Sandbox Code Playgroud)
但是,当我去检查文件时,我看到645?.tsv
了例如.我最初认为这是tsv文件中的行分隔符(包含日期和随机的那个),但它在同一个unix文件系统中运行,它不是从dos到unix的事务来自文件的一些行:
head dates_randoms.tsv
20141101 356
20141102 604
20141103 680
20141104 668
20141105 995
20141106 946
20141107 354
20141108 234
20141109 429
20141110 384
Run Code Online (Sandbox Code Playgroud)
有什么建议?
因此,假设我们有一个包含1亿个数字的巨大文本文件,需要10分钟才能处理并存储到ArrayList中.使用eclipse,如何将此列表存储到内存中,因此每次测试代码时都不需要花10分钟处理文本文件.
我已经尝试将列表写入文件并执行此操作:
//store the contents of the text file as a list object in C:/filepath
FileInputStream fis = new FileInputStream("C:/filepath");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Integer> newList = (ArrayList<Integer>)ois.readObject();
ois.close();
//process newList
Run Code Online (Sandbox Code Playgroud)
但这可能是非常低效的,大概是因为文件没有存储到内存中.
有没有一个技巧能够有效地做到这一点,只需处理一次文本文件,仍然在许多不同的版本中使用处理时间的成果?
在Java应用程序中,我有一个类的方法,只使用其名称保存文件.
public void doSomething(){
final File file = new File("XXX"+(new Random().next())+".txt");
file.createNewFile();
}
Run Code Online (Sandbox Code Playgroud)
然后,在使用JUnit 4的单元测试中,我运行执行该方法的类,并XXX.txt
在项目的根文件夹中看到使用name创建的文件.
@Test
public void doSomethingTest() throws Exception{
//call doSomething();
Path path = //to project folder
Files.delete(path);
}
Run Code Online (Sandbox Code Playgroud)
如何动态获取此路径,以便我可以使用@After
方法删除它?
我有一个文件weights01.txt,它填充了4x3矩阵中的浮点数,如下所示
1.1 2.123 3.4
4.5 5 6.5
7 8.1 9
1 2 3.1
Run Code Online (Sandbox Code Playgroud)
我正在尝试读取此文件并将数据传输到名为newarray的数组.这是我正在使用的代码:
int main()
{
ofstream myfile;
float newarray[4][3];
myfile.open ("weights01.txt");
for(int i = 0 ; i < 4; i++) // row loop
{
for(int j = 0 ; j < 3; j++) // column loop
{
myfile >> newarray[i][j]; // store data in matrix
}
}
myfile.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的线路出错了
myfile >> newarray[i][j];
Run Code Online (Sandbox Code Playgroud)
错误:'myfile >>中没有匹配'operator >>'newarray [i] [j]'
我不明白为什么会出现这个错误
我搜索了之前关于这个"不匹配'运算符>>'错误的问题,包括这个和这个.我也读过这个关于重载运算符的长篇讨论,但我没有找到解释(可能是因为我之前没有使用过很多文件而且没有我真的关注正在发生的事情.
我是Python的新手,但直到现在我还不知道。我在for循环中有一个基本程序,该程序从站点请求数据并将其保存到文本文件中,但是当我在任务管理器中检查时,发现内存使用量只会增加吗?长时间运行时,这可能对我来说是个问题。这是Python的标准做法,还是可以更改?这是程序的基本内容
savefile = open("file.txt", "r+")
for i in savefile:
#My code goes here
savefile.write(i)
#end of loop
savefile.close()
Run Code Online (Sandbox Code Playgroud) 我写的东西就像数据库,但相当简单和小.所以我使用了一个dict,然后将其保存在一个文件中.
代码大致是:
d = {'apple': 1, 'bear': 2}
print(d, file=f)
Run Code Online (Sandbox Code Playgroud)
我希望它在下次运行时导入,即从文件导入它作为字典,无论如何要做到这一点?
我正在尝试编写一个实现自己的自定义排序的perl脚本.如果输入文件如下所示:
hello
advantage
cat
Run Code Online (Sandbox Code Playgroud)
排序版本看起来像:
cat
hello
advantage
Run Code Online (Sandbox Code Playgroud)
如果单词按长度排序,如果两个单词的长度相同,那么它就会对两者进行正常的词典比较.
基于我在网上看到的东西,我的排序行看起来像这样:
@sorted = sort { length $a <=> length $b } @elements
Run Code Online (Sandbox Code Playgroud)
但我不确定如果两个单词的长度相同,我会如何添加正常排序的部分.
我有一个存储整数序列的文件.总的整数是未知的,所以如果我从文件中读取一个整数,我继续使用malloc()来应用新的内存.我不知道我是否可以继续询问内存并在数组末尾添加它们.Xcode一直警告我malloc()行中的'EXC_BAD_EXCESS'.如果我继续从文件中读取整数,我怎么能这样做?
int main()
{
//1.read from file
int *a = NULL;
int size=0;
//char ch;
FILE *in;
//open file
if ( (in=fopen("/Users/NUO/Desktop/in.text","r")) == NULL){
printf("cannot open input file\n");
exit(0); //if file open fail, stop the program
}
while( ! feof(in) ){
a = (int *)malloc(sizeof(int));
fscanf(in,"%d", &a[size] );;
printf("a[i]=%d\n",a[size]);
size++;
}
fclose(in);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的python脚本,它应该打印一个像这样传递的文件的内容:python script.py stuff.txt
.我没有得到任何输出.
这是代码:
import sys
fname = sys.argv[1]
f = open(fname, 'r')
f.read()
Run Code Online (Sandbox Code Playgroud)
从我所读到的,这应该是有效的.为什么不?
您能否帮助我如何仅过滤以“ O_”开头的文件?对我来说StartsWith和EndsWith方法不起作用,结果始终是一个空列表。
Files.walk(Paths.get(SOURCEDIR)).filter(Files::isRegularFile).forEach(filePath -> {
if (true){
System.out.println(filePath.getFileName());
}
});
Run Code Online (Sandbox Code Playgroud)