简体   繁体   English

如何通过读取 javascript 中的文本文件来创建数组

[英]how to create an array by reading text file in javascript

I have a text file with profanity word separated by comma (,).我有一个带有用逗号 (,) 分隔的亵渎词的文本文件。 Now I have to create an array by reading this text file, each and every profanity word should be stored into the array.现在我必须通过读取这个文本文件来创建一个数组,每个脏话都应该存储到数组中。 if someone know how to do this please reply me as soon as possible.如果有人知道如何做到这一点,请尽快回复我。 Note: I have to stored this file anywhere it's my choice.注意:我必须将此文件存储在我选择的任何地方。 The only thing I have to do is, read the text file by just giving complete path of file.我唯一要做的就是通过提供完整的文件路径来读取文本文件。 It is not necessary the solution should be in javascript you can reply with jquery or ajax solution.解决方案不必在 javascript 中,您可以使用 jquery 或 ajax 解决方案进行回复。 thank you.谢谢你。

you can't do IO operations from Javascript,您不能从 Javascript 执行 IO 操作,

you can probably try ajax你可以试试 ajax

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

or jquery load或 jquery 负载

http://api.jquery.com/load/ http://api.jquery.com/load/

Here is a jQuery + AJAX Solution that you can use这是您可以使用的 jQuery + AJAX 解决方案

//HTML
<input id="file" type="file" />
<input type="button" id="load" value="Load CSV" />

//JavaScript/jQuery
$(function () {

  //Stop caching if csv is likely to change often (change $.get to $.ajax)
  $.ajaxSetup({
     cache: false
  });

  $('#load').click(function () {

     $.get($('#file').val(), function (content) {
        var output = content.split(new RegExp(",|\r"))
                            .map(function (element) {
           //Do what ever tidy up here
           return $.trim(element).toLowerCase();
        });
        console.log(output);
     });

  });

});

Tested on CSV from http://en.wikipedia.org/wiki/Comma-separated_valueshttp://en.wikipedia.org/wiki/Comma-separated_values的 CSV 上测试

You could hide the file input control by using something like this http://plugins.jquery.com/project/custom-file您可以使用类似http://plugins.jquery.com/project/custom-file的东西来隐藏文件输入控件

see this resource and code, this is a really great post about this issue看到这个资源和代码,这是一篇关于这个问题的非常好的帖子

http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm http://www.bennadel.com/blog/1504-Ask-Ben-Parsing-CSV-Strings-With-Javascript-Exec-Regular-Expression-Command.htm

Technically you can do by - http://www.phpletter.com/Our-Projects/AjaxFileUpload/从技术上讲,您可以通过- http://www.phpletter.com/Our-Projects/AjaxFileUpload/

  1. Upload file to your server side code, parse it back with json array将文件上传到您的服务器端代码,使用 json 数组将其解析回来
    def upload_file    
       data = params['fileToUpload'].read
       render :json => data.split(',')
    end
  1. In your client code在您的客户端代码中

< input type='file' size='25' name='fileToUpload'>

 <button onclick='return ajaxFileUpload();'>Ajax Read</button>

  1. In your ajaxFileUpload() method to handle the return data.split(',')在您的 ajaxFileUpload() 方法中处理返回的 data.split(',')

Are you able to manipulate the CSV file manually before reading it?您是否能够在阅读 CSV 文件之前手动操作它? I had a similar issue, but was able to get the person that generated it to do a few things to it such as doing a global search/replace for '\' then wrapping the contents in a JS string variable.我遇到了类似的问题,但能够让生成它的人对其做一些事情,例如对'\'进行全局搜索/替换,然后将内容包装在 JS 字符串变量中。 Then I just included the new file as I would any other JS file.然后我就像包含任何其他 JS 文件一样包含新文件。

Ex.前任。

<!-- Modified CSV file with single JS variable -->
<script type="text/javascript" src="csvFile.js"></script>
<!-- Ben Nadel CSV Parser and any other custom code -->
<script type="text/javascript" src="parseCsv.js"></script>

Original CSV:原装CSV:

word,"multiple words",apostrophe's

Modified JS CSV:修改JS CSV:

var csvData = 'word,"multiple words",apostrophe\'s';

And then I used the Ben Nadel link posted by samccone to do the actual parsing.然后我使用 samccone 发布的 Ben Nadel 链接进行实际解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM