我可以在控制台中为我的Rails应用程序运行以下命令,并将CSV文件导入我的数据库.
require 'csv'
# row will be an array with the fields in the order they appear in the file
CSV.open('myfile.csv', 'r') do |row|
# assuming the fields in the CSV file are in order npa, nxxFrom, nxxTo, trnk
# create and save a Trunk model for each row
Trunk.create!(:npa => row[0], :nxxFrom => row[1], :nxxTo => row[2], :trnk => row[3])
end
Run Code Online (Sandbox Code Playgroud)
但是我想通过为它创建一个脚本来促进这个过程.问题是我不知道如何编写特定于应用程序的脚本.为了运行上述命令,我需要通过以下方式在应用程序文件夹中启动控制台:
ruby script/console
Run Code Online (Sandbox Code Playgroud)
因此,只需将命令复制/粘贴到.rb文件中,执行就不起作用.
任何帮助将永远受到赞赏:)
如何快速找到数组中某行元素的最小或最大和?
例如:
1, 2
3, 4
5, 6
7, 8
Run Code Online (Sandbox Code Playgroud)
最小和为第0行(1 + 2),最大和为第3行(7 + 8)
print mat.shape
(8, 1, 2)
print mat
[[[-995.40045 -409.15112]]
[[-989.1511 3365.3267 ]]
[[-989.1511 3365.3267 ]]
[[1674.5447 3035.3523 ]]
[[ 0. 0. ]]
[[ 0. 3199. ]]
[[ 0. 3199. ]]
[[2367. 3199. ]]]
Run Code Online (Sandbox Code Playgroud) 如果路径包含"release"和"arm-linux",我需要阻止os.walk进一步向下移动.我在不同级别的目录中有一堆这些.所以我不能简单地决定水平.到目前为止,我有以下内容,它不必要地潜入'arm-linux'中的目录.
def main(argv):
for root, dirs, files in os.walk("."):
path = root.split(os.sep)
if "release" and "arm-linux" in path:
print(os.path.abspath(root))
getSharedLib(argv)
Run Code Online (Sandbox Code Playgroud)
[ 更新 ]这是我的解决方案
def main(argv):
for root, dirs, files in os.walk("."):
path = root.split(os.sep)
if "release" in path and "arm-linux" in path:
print(os.path.abspath(root))
getSharedLib(argv)
del dirs[:]
Run Code Online (Sandbox Code Playgroud) 我正在开发一个嵌入式 Linux 发行版,目前我必须与我的团队共享内核及其内核模块。通常我会使用 Yocto/Openembedded 框架,但现在我只能使用 Linux Makefile。有没有办法生成内核模块的 tarball?
我是一个noob Perl用户试图尽快完成我的工作,所以我今天可以准时回家:)
基本上我需要在文本文件中打印下一行空白行.
以下是我到目前为止的情况.它可以完美地定位空白行.现在我只需打印下一行.
open (FOUT, '>>result.txt');
die "File is not available" unless (@ARGV ==1);
open (FIN, $ARGV[0]) or die "Cannot open $ARGV[0]: $!\n";
@rawData=<FIN>;
$count = 0;
foreach $LineVar (@rawData)
{
if($_ = ~/^\s*$/)
{
print "blank line \n";
#I need something HERE!!
}
print "$count \n";
$count++;
}
close (FOUT);
close (FIN);
Run Code Online (Sandbox Code Playgroud)
谢谢一堆:)