我有一个文本文件(list.txt),其中包含单个和多个单词的英语短语。我的目标是对每个单词进行字数统计并将结果写入 CSV 文件。
我已经弄清楚了写入每个单词的唯一实例数量的命令,按从最大到最小的顺序排序。该命令是:
$ tr 'A-Z' 'a-z' < list.txt | tr -sc 'A-Za-z' '\n' | sort | uniq -c | sort -n -r | less > output.txt
Run Code Online (Sandbox Code Playgroud)
问题在于新文件(output.txt)的格式化方式。前导有 3 个空格,后面是出现次数,后面是一个空格,最后是单词。然后转到下一行。例子:
9784 the
6368 and
4211 for
2929 to
Run Code Online (Sandbox Code Playgroud)
我需要做什么才能以更理想的格式(例如 CSV)获得结果?例如,我希望它是:
9784,the
6368,and
4211,for
2929,to
Run Code Online (Sandbox Code Playgroud)
更好的是:
the,9784
and,6368
for,4211
to,2929
Run Code Online (Sandbox Code Playgroud)
有没有办法使用 Unix 命令来执行此操作,或者我是否需要在文本编辑器或 Excel 中进行一些后处理?
在一个java项目中,我生成一个大的csv文件(大约500 Mb),我需要将该文件拆分为多个文件,每个文件最多10 Mb大小。我发现很多类似的帖子,但其中任何一个都回答了我的问题,因为在所有帖子中,java 代码都将原始文件拆分为 10 Mb 文件,并且(显然)截断了记录。相反,我需要每条记录都是完整的、完好无损的。任何记录都应该被截断。如果我将一条记录从原始大 csv 文件复制到一个生成的文件,并且如果我复制该记录,文件尺寸将溢出 10 Mb,我应该能够不复制该记录,关闭该文件,创建一个新文件并将该记录复制到新记录中。是否可以?有人能帮我吗?谢谢你!
我尝试了这段代码:
File f = new File("/home/luca/Desktop/test/images.csv");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
FileOutputStream out;
String name = f.getName();
int partCounter = 1;
int sizeOfFiles = 10 * 1024 * 1024;// 1MB
byte[] buffer = new byte[sizeOfFiles];
int tmp = 0;
while ((tmp = bis.read(buffer)) > 0) {
File newFile=new File("/home/luca/Desktop/test/"+name+"."+String.format("%03d", partCounter++));
newFile.createNewFile();
out = new FileOutputStream(newFile);
out.write(buffer,0,tmp);
out.close();
}
Run Code Online (Sandbox Code Playgroud)
但显然不行。此代码将源文件拆分为 n 个 10Mb 文件并截断记录。就我而言,我的 csv 文件有 16 列,因此通过上述过程,我的最后一条记录仅填充了 5 …
我是 Powershell 的新手,但我已经尽力了。我正在尝试创建一个脚本来将文件复制到阵列中所有 XP 计算机的所有用户桌面。该脚本基本上是说“如果机器可 ping 通,则复制文件,如果不是,则不复制。” 然后我想将此信息导出到 CSV 文件中以供进一步分析。
我已经设置了一切,但无论我做什么,它都只会导出它运行的最后一台电脑。它似乎运行在所有 PC 上(通过输出到 txt 文件进行测试),但它不会将所有计算机记录到 CSV。任何人都可以提供任何建议吗?
$ArrComputers = "PC1", "PC2", "PC3"
foreach ($Computer in $ArrComputers) {
$Reachable = Test-Connection -Cn $Computer -BufferSize 16 -Count 1 -ea 0 -quiet
$Output = @()
#Is the machine reachable?
if($Reachable)
{
#If Yes, copy file
Copy-Item -Path "\\servername\filelocation" -Destination "\\$Computer\c$\Documents and Settings\All Users\Desktop\filename"
$details = "Copied"
}
else
{
#If not, don't copy the file
$details = "Not Copied"
}
#Store the information from …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用工作台将 csv 上传到 MySQL,到目前为止,我所有的尝试都被证明是徒劳的。
我最初尝试使用“上传”功能,但它抱怨我拥有的任何空/空字段。
我现在尝试使用这个功能:
LOAD DATA infile 'C:\\temp\\SubEq.csv'
INTO TABLE cas_asset_register.subequipment
fields terminated BY ','
lines terminated BY '\n'
(seid, parentme, @vparentse, name, status, description, equipmenttype, comments, removed, active, @vsupplierid)
SET
ParentSE = nullif(@vparentse,''),
SupplierId = nullif(@vsupplierid,'')
;
Run Code Online (Sandbox Code Playgroud)
但它似乎又在抱怨(可能)同样的事情:
错误代码:1261。第 1 行不包含所有列的数据
我已经查看了这个问题和这个问题的答案,但都没有解决我的问题。
表创建查询:
CREATE TABLE `subequipment` (
`SEId` int(11) NOT NULL AUTO_INCREMENT,
`ParentME` int(11) DEFAULT NULL,
`ParentSE` int(11) DEFAULT NULL,
`Name` varchar(255) DEFAULT NULL,
`Status` varchar(100) DEFAULT NULL,
`Description` varchar(255) DEFAULT NULL,
`EquipmentType` …Run Code Online (Sandbox Code Playgroud) 有很多教程可用于将 SQLite 数据库导出到 csv 文件,但没有足够的东西从房间数据库导出。
使用 sqlite 导出参考将SQLite 数据库导出到 android 中的 csv 文件,手动解析每列行的空间。以下是我的代码:
@Dao
interface CategoryDao {
@Query("SELECT * FROM Category")
fun getAllCategory(): List<Category>
}
// Export csv logic
val categoryList = categoryDao.getAllCategory()
val csvWrite = CSVWriter(FileWriter(file))
for (item in categoryList) {
val arrStr = arrayOf<String>(item.categoryId, item.categoryName)
csvWrite.writeNext(arrStr)
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以导出csv。即使在房间里也没有实用地获取表的列名,因此无法为所有表创建动态通用逻辑。
我在 S3 上有一个非常简单的 csv 文件
"i","d","f","s"
"1","2018-01-01","1.001","something great!"
"2","2018-01-02","2.002","something terrible!"
"3","2018-01-03","3.003","I'm an oil man"
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下命令创建一个表
CREATE EXTERNAL TABLE test (i int, d date, f float, s string)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'
LOCATION 's3://mybucket/test/'
TBLPROPERTIES ("skip.header.line.count"="1");
Run Code Online (Sandbox Code Playgroud)
当我查询表 ( select * from test) 时,出现如下错误:
HIVE_BAD_DATA:
解析字段 1 的字段值“ 2018-01-01”时出错:对于输入字符串:“2018-01-01”
更多信息:
d列更改为字符串,则查询将成功该文档明确暗示这是支持的。寻找遇到此问题的任何人,或任何建议。
public interface ICsvProductReaderConfigurationFactory
{
Configuration Build();
}
public class CsvProductReaderConfigurationFactory : ICsvProductReaderConfigurationFactory
{
private readonly ClassMap<ProductDto> classMap;
public CsvProductReaderConfigurationFactory(IProductDtoClassMapProvider classMapProvider)
{
classMap = classMapProvider.Get();
}
public Configuration Build()
{
var config = new Configuration
{
Delimiter = "\t",
HasHeaderRecord = true,
IgnoreQuotes = true,
MissingFieldFound = (rows, fieldIndex, readingContext) =>
Log.Warn($"Missing Field Found at line {readingContext.Row}\r\n" +
$"Field at index {fieldIndex} does not exist\r\n" +
$"Raw record: {readingContext.RawRecord}"),
BadDataFound = context =>
Log.Warn($"Bad data found at row {context.Row}\r\n" +
$"Raw …Run Code Online (Sandbox Code Playgroud) 我正在使用谷歌云功能在谷歌云存储上创建一个 CSV 文件。现在我想编辑该文件 - 是否可以在该文件中附加数据?如果是,那么如何?
csv google-cloud-storage google-cloud-platform google-cloud-functions
我正在尝试将数据集映射到具有不同标题的空白 CSV 文件,因此我实际上是在尝试将数据从一个具有不同标题的 CSV 文件映射到具有不同标题数量并称为不同内容的新 CSV,原因这个问题是不同的,因为列名不一样,但也没有重叠的列。而且我不能用新的标题覆盖数据文件,因为数据文件有其他列包含不相关的数据,我确定我把它复杂化了。
我已经看过这个示例代码,但是我如何更改它,因为这个示例使用一个公共头来连接数据。
a = pd.read_csv("a.csv")
b = pd.read_csv("b.csv")
#a.csv = ID TITLE
#b.csv = ID NAME
b = b.dropna(axis=1)
merged = a.merge(b, on='title')
merged.to_csv("output.csv", index=False)
Run Code Online (Sandbox Code Playgroud)
样本数据
a.csv(空白格式文件,格式必须与此文件匹配):
Headers: TOWN NAME LOCATION HEIGHT STAR
Run Code Online (Sandbox Code Playgroud)
b.csv:
Headers: COUNTRY WEIGHT NAME AGE MEASUREMENT
Data: UK, 150lbs, John, 6, 6ft
Run Code Online (Sandbox Code Playgroud)
预期输出文件:
Headers: TOWN NAME LOCATION HEIGHT STAR
Data: (Blank) John, UK, 6ft (Blank)
Run Code Online (Sandbox Code Playgroud) 我从供应商那里得到了一个“csv 文件”(使用他们的 API),但他们所做的只是把整个事情都吐到他们的响应中。这不会是一个重大问题,当然,除了那些讨厌的人中的一些输入数据并放入诸如换行符之类的“功能”之外。我现在正在做的是为原始数据创建一个文件,然后重新打开它以读取数据:
open RAW, ">", "$rawfile" or die "ERROR: Could not open $rawfile for write: $! \n";
print RAW $response->content;
close RAW;
my $csv = Text::CSV_XS->new({ binary=>1,always_quote=>1,eol=>$/ });
open my $fh, "<", "$rawfile" or die "ERROR: Could not open $rawfile for read: $! \n";
while ( $line = $csv->getline ($fh) ) { ...
Run Code Online (Sandbox Code Playgroud)
不知何故,这似乎……不雅。似乎我应该能够从 $response->content (多行字符串)中读取数据,就好像它是一个文件一样。但我对如何做到这一点完全空白。一个指针将不胜感激。谢谢,保罗