最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Read a csv file in Typescript - Stack Overflow

programmeradmin1浏览0评论

I'm getting into TypeScript as a way to write simple utility programs, meant to be run on the mand line. There are a bunch of basic questions I have, maybe someone can give me pointers.

  1. I am assuming that I will run the app by a mand like: $ node myapp.js
  2. If I want to run it by $ myapp.js I need a shebang line in the js, but I can't see how to have that flow from the .ts file, so I have to post edit the js file, right?
  3. How do I just open a text file from a local path within TypeScript? Where do I look for the right incantation?
  4. And once I have the text file open I have to process it as a cvs. I found a library called node-cvs but I am not sure how to connect the dots.

I'm getting into TypeScript as a way to write simple utility programs, meant to be run on the mand line. There are a bunch of basic questions I have, maybe someone can give me pointers.

  1. I am assuming that I will run the app by a mand like: $ node myapp.js
  2. If I want to run it by $ myapp.js I need a shebang line in the js, but I can't see how to have that flow from the .ts file, so I have to post edit the js file, right?
  3. How do I just open a text file from a local path within TypeScript? Where do I look for the right incantation?
  4. And once I have the text file open I have to process it as a cvs. I found a library called node-cvs but I am not sure how to connect the dots.
Share edited Feb 12, 2023 at 22:30 Rami Alshareef 7,17013 gold badges50 silver badges77 bronze badges asked Jun 19, 2015 at 2:08 pitosalaspitosalas 10.9k15 gold badges77 silver badges132 bronze badges 1
  • 1 It should be CSV (Comma Separated Values) not CVS in the title. – Meryan Commented Dec 7, 2021 at 16:30
Add a ment  | 

2 Answers 2

Reset to default 6
  1. I am assuming that I will run the app by a mand like: $ node myapp.js

Yes. TypeScript piles to JavaScript.

  1. If I want to run it by $ myapp.js I need a shebang line in the js, but I can't see how to have that flow from the .ts file, so I have to post edit the js file, right?

No. Take a look at my workaround : https://github./npm/npm/issues/6674#issuement-108132800

  1. How do I just open a text file from a local path within typescript? Where do I look for the right incantation?

fs.readFile in node. You can use it in typescript (and even provide type safety using node.d.ts : https://github./borisyankov/DefinitelyTyped/blob/master/node/node.d.ts

  1. And once I have the text file open I have to process it as a cvs. I found a library called node-cvs but I am not sure how to connect the dots.

You can just do require('node-cvs') same as javascript

You would need to do something like:

/// <reference path="node.d.ts" />
/// <reference path="a-type-definition-for-node-csv.d.ts" />

import fs = require('fs');
import parse = require('node-csv');

var parser = parse({delimiter: ','}, function(err, data) {
    console.log(data);
});

fs.createReadStream(__dirname+'/fs_read.csv').pipe(parser);
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>