【发布时间】:2021-07-02 14:10:41
【问题描述】:
我认为这是一个简单的任务,但我是一个只懂一点代码的生物学家,经过几天的尝试,我无能为力:'(
在 Mac 上使用终端。我有一个 CSV 文件,我想按行(162 行)将其拆分为单独的文件,并且我想通过第一列和第二列(genus_species)的内容来命名文件。然后我需要将所有 162 个 genus_species 保存为 HTML 文件。
我只尝试了 Ruby 的“拆分”部分(来自 StackExchange/overflow 的推荐)。以下是我的一些尝试。他们是乐于助人的论坛的科学怪人,每次我都对它为什么不起作用发表一点评论。
示例 HTML
<!DOCTYPE html>
<html><head>
<meta charset="UTF-8">
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script></head>
<body>
<h1><em><!-- Species name --></em> - <!-- Common name --></h1>
<h2>Status</h2>
<p></p>
<h2>Info</h2>
<p></p>
<h2>Time of year this bee is seen</h2>
<p></p>
<h2>Identification</h2>
<p></p>
<h3>Similar Species</h3>
<p></p>
<h2>Flowers</h2>
<p></p>
<h2>Sociality</h2>
<p></p>
<h2>Nest</h2>
<p></p>
<div id="refs" class="references">
--<br>More information:<br> <!-- <a href="https://bugguide.net/node/view/70932">Bug Guide</a> --></div>
</body></html>
更多信息基于评论
以下是从文本文件中复制的一些行:
Genus,species,Common name,Status,Info,Time of year this bee is seen,Identification,Similar Species,Flowers,Sociality,Nest,Bug Guide,Discover Life,Other,
Agapostemon,melliventris,Honey-tailed Striped-Sweat bee,Secure G5,Excavates into deep burrows in ground nests,March-December,Agapostemon males have black and yellow stripes on the abdomen. Females have a yellow band on the lower margin of the clypeus.,All other Agapostemon species,Wide variety of plants,Solitary,"Deep, underground excavation",https://bugguide.net/node/view/70932,https://www.discoverlife.org/20/q?search=Agapostemon+melliventris,https://explorer.natureserve.org/Taxon/ELEMENT_GLOBAL.2.928401/Agapostemon_melliventris,
Agapostemon,sericeus,Silky Striped Sweat Bee,Secure G5,"Not choosy about lawn, as long as flowers are present",April-October,Agapostemon males have black and yellow stripes on the abdomen. A. sericeus males have a tooth on its hind femur. Female has metallic green abdomen.,All other Agapostemon species,Wide variety of plants,Solitary,Ground-nester in loamy soils,https://bugguide.net/node/view/83023,https://www.discoverlife.org/mp/20q?search=Agapostemon+sericeus,https://www.sharpeatmanguides.com/sweat-bees,
Agapostemon,splendens,Brown-winged Striped-Sweat Bee,Secure G5,This is the most common Agapostemon found in the southeast region,April-October,Agapostemon males have black and yellow stripes on the abdomen. A. splendens have brown wings. The female abdomen is often somewhat bluish.,All other Agapostemon species,"Jacquemontia reclinata, wide variety of plants",Solitary,Ground-nester in sandy soils,https://bugguide.net/node/view/74478,https://www.discoverlife.org/mp/20q?search=Agapostemon+splendens,,
更新了我基于 cmets 尝试过的代码。 这行得通,我认为它正朝着我想要的方向前进,但在终端窗口中很难说:
f = File.new("bee_key_fact_sheet .csv")
f.each_line { |line| puts line }
Currently playing with some kind of File.write line to add here and then close?
尝试 #1
file = File.open("bee_key_fact_sheet.csv")
awk
'(NR==1){header=$0;next}
(NR%l==2) {
close(file);
file=sprintf("%s.%0.5d.csv",FILENAME,++c)
sub(/csv[.]/,"",file)
print header > file
}
{f.write}'
File.close
#AWK 无法识别,要求“显示所有可能性 (y/n)” 我尝试返回“y”和“yes”,但两次都说我的答案无法识别
尝试 #2
file_data = File.read("bee_key_fact_sheet.csv").split
#这可行,但每个逗号分开
尝试 #3
file_data = File.foreach("bee_key_fact_sheet.csv") { |line| puts line}.split
#这返回的东西比用每个逗号分割稍微少一些,但得到这个错误消息“undefined method `split' for nil:NilClass”
尝试 #4
bee_key_fact_sheet.csv.foreach('so1.csv', :headers => true, :col_sep => ",", :skip_blanks => true) do |row|
id, name = row[0], row[1]
unless (id =~ /#/)
names = name.split
end
#这没有返回任何内容
非常感谢您花时间阅读本文。
【问题讨论】:
-
为什么这个问题用
bash标记? -
请添加几行您的 CSV 文件以及您的“拆分”文件名和内容应该是什么
-
对于@Fravadona 的评论,如果您要显示示例 CSV 文件的全部内容(包括标题行,如果有的话),读者会发现它非常有帮助,示例如下可能(根据字段和行的数量),同时保留文件的结构。然后提供要创建的文件的示例。
-
大家好!我添加了表格的屏幕截图。 @cyrus 抱歉,我补充说,因为到目前为止对我有帮助的一些论坛已将其标记为(掌心)。
-
CSV 是一个文本文件。使用基本的文本编辑器(如 Windows 上的 Notepad.exe)打开它,并将其前 3 行放在此处。另外,你想用它创建 HTML 文件,你有例子吗?