我正在尝试从csv文件中提取信息,该文件有很多行,我想只从特定行返回一些值/列。我使用papa / babyparse将csv文件转换为JSON,但很难显示/提取特定行。
var baby = require('babyparse');
var csv2 = baby.parseFiles("netreqs.csv",{
header:true,
skipEmptyLines: true,
step: function(row) {
console.log("Row:", row.data);
},
complete: function() {
console.log("All done!");
}
});
我得到的输出似乎是不错的JSON。
Row: [ { Req: 'RQ0342384',
'Requestor country': 'UK',
ReqType: 'other',
'ATOS Approved': '21.09.2016',
Urgent: 'No',
Assignee: 'Hans Gans',
'Change number': 'NA',
'Implementation Date': '',
'Change fully approved': 'No',
'Completion Date': '',
'Req Closed': 'No' } ]
Row: [ { Req: 'RQ0343423',
'Requestor country': 'US',
ReqType: 'Firewall',
'ATOS Approved': '04.11.2016',
Urgent: 'No',
Assignee: 'Peter Mueller',
'Change number': 'C9343449',
'Implementation Date': '',
'Change fully approved': 'No',
'Completion Date': '31.01.2017',
'Req Closed': 'No' } ]
...
我尝试将row.data.req
用于我的“if”,但得到一个“未定义”的回复。还尝试了.filter
和.hasOwnProperty
,但不知怎的,我似乎错过了一些东西(在if之前尝试JSON.stringify
但没有成功)。经过几个小时的追踪和错误和谷歌搜索我想我在这里问。
Idealy我能够使用变量来过滤Req的“行”(这是我从另一个函数得到的输入),然后从我想要实现的“行”中查询其他键/值对基于数据的不同反应。
我必须承认我对此很新,感谢您的支持。非常感谢
答案 0 :(得分:0)
我相信你不需要使用解析器。只需要使用readLine:
public class Parser extends AsyncTask<Void,Integer,Integer> {
Context c;
ListView lv;
String data;
ArrayList<String> categories = new ArrayList<>();
ProgressDialog pd;
public Parser(Context c, String data, ListView lv) {
this.c = c;
this.data = data;
this.lv = lv;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(c);
pd.setTitle("Parsing Data");
pd.setMessage("Please Wait...");
pd.show();
}
@Override
protected Integer doInBackground(Void... params) {
return this.parse();
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
if(integer == 1)
{
//ADAPTER
ArrayAdapter<String> adapter = new ArrayAdapter<String>(c, android.R.layout.simple_list_item_1, categories);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0){
}
}
});
}else
{
Toast.makeText(c,"Unable to Parse",Toast.LENGTH_SHORT).show();
}
pd.dismiss();
}
//PARSE RECEIVED DATA
private int parse(){
try
{
//ADD THAT DATA TO JSON ARRAY FIRST
JSONArray ja = new JSONArray(data);
//CREATE JO OBJECT TO HOLD A SINGLE ITEM
JSONObject jo = null;
categories.clear();
//LOOP THROUGH ARRAY
for(int i =0 ; i<ja.length();i++)
{
jo = ja.getJSONObject(i);
//RETRIEVE NAME
name=jo.getString("item_name");
price=jo.getString("item_price");
//ADD TO ARRAY LIST
categories.add(name + " " + price);
}
return 1;
} catch (JSONException e) {
e.printStackTrace();
}
return 0;
}
我希望它有所帮助。
修改:强>
如果您需要特定值,可以设置不同的条件:
var output = [];
var count = 0
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('file.csv')
});
lineReader.on('line', function (line) {
var jsonFromLine = {};
if (count == 2) { // this is my conditional. Set line 2
var lineSplit = line.split(',');
// select columns you want
jsonFromLine.column0 = lineSplit[0];
jsonFromLine.column1 = lineSplit[1];
// ...
output.push(jsonFromLine);
}
count++;
});
lineReader.on('close', function (line) {
console.log(output); // list output
});
这对我来说很好用
<强> Edit.2:强>
您也可以使用承诺:
var output = [];
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream('file.csv')
});
lineReader.on('line', function (line) {
var jsonFromLine = {};
var lineSplit = line.split(',');
// select columns you want
jsonFromLine.req = lineSplit[0];
jsonFromLine.column1 = lineSplit[1];
// ...
if (jsonFromLine.req === 'RQ0191223') {
output.push(jsonFromLine);
}
});
lineReader.on('close', function (line) {
console.log(output); // list output
});
我希望它有所帮助