简体   繁体   English

使用Regexp从带有节点的csv文件中删除\\ n和\\ r

[英]Removing \n and \r with Regexp from csv file with node

I'm trying to read the contents of a .csv file that has city information, as such : 我正在尝试读取具有城市信息的.csv文件的内容,例如:

Paris,2138551,5.272,1,138,900,48.864716,2.349014
Marseille,794811,1.959,423,439,43.29695,5.38107
Lyon,472317,1.164,251,730,45.74846,4.84671
Toulouse,433055,2.135,230,826,43.60426,1.44367
Nice,338620,1.669,180,545,43.675819,7.289429
Nantes,277269,1.367,147,879,47.21725,-1.55336

So I want to parse the file, and transform it into an array (not an object) with .split(','). 所以我想解析该文件,并使用.split(',')将其转换为数组(而不是对象)。

Here's my problem, when I'm using Regexp to remove these damn \\n and \\r with the following regex, it doesn't work, they are still bugging my output : 这是我的问题,当我使用Regexp使用以下正则表达式删除这些该死的\\ n和\\ r时,它不起作用,它们仍在错误地检查我的输出:

var cities = data.replace(/\n/g, '').replace("'", '').split(',');

Then, when I use this regex, it removes all the "," as well ! 然后,当我使用此正则表达式时,它也会删除所有的“,”! So I tried replacing them with "_", which works, but then I have another problem, it also removes the minus sign (-) from the latitude and longitude : 因此,我尝试用“ _”替换它们,这很有效,但是我还有另一个问题,它也从纬度和经度中删除了减号(-):

var cities = await data.replace(/(?:\\[rn]|[\r\n]+)+/g,
'_').replace(/[^\w\s]/gi, '').split('_');

(btw the "_" doesnt appear in the code above on split idk why) (顺便说一句,“ _”没有出现在上面的代码中,用于拆分idk)

How can i Remove the \\n and \\s like the second regex does BUT without touching the "," and "-" ? 我如何像不使用第二个正则表达式那样删除\\ n和\\ s,而无需触摸“,”和“-”?

I'm really not into Regex. 我真的不喜欢Regex。

EDIT: I've used one of the solutions bellow, but the output is still bugged, meaning that blank lines are still not removed by the following line : var cities = await data.replace(/(?:\\[rn]|[\\r\\n]+)+/g, ',').replace(/[^\\w\\s,-]/gi, ',').split(','); 编辑:我已经使用了以下解决方案之一,但是输出仍然有问题,这意味着空白行仍不能被以下行删除:var city = await data.replace(/(?(\\:?\\ [rn] | [ \\ r \\ n] +)+ / g,',')。replace(/ [^ \\ w \\ s,-] / gi,',')。split(',');

There must still be /n or /r despite the regex above. 尽管上面的正则表达式仍然必须存在/ n或/ r。 Anyone knows how to get rid of the lines ? 有人知道如何摆脱界限吗?

the output is bugged as u can see : 如您所见,输出已错误:

console.log('City: '+i+' sur '+cities.length+' - "'+cities[i]+'", "'+countryName+'", '+cities[ii]+', '+cities[iii]+', '+cities[iv]+', '+cities[v]+', '+cities[vi]+'\\n'); console.log('City:'+ i +'sur'+ cities.length +'-“'+ cities [i] +'”,“'+ countryName +'”,'+ cities [ii] +','+ cities [ iii] +','+ cities [iv] +','+ cities [v] +','+ cities [vi] +'\\ n');

City: 0 sur 1095 - " New York City", "United States", 8537673, 31, 709, 34, 245 市:0 sur 1095年-“纽约市”,“美国”,8537673、31、709、34、245

City: 6 sur 1095 - "881", "United States", 40, 730610, -73, 935242 , Los Angeles 城市:10sur 1095-“ 881”,“美国”,40,730610,-73,935242,洛杉矶

City: 12 sur 1095 - "3971883", "United States", 14, 752, 15, 931, 949 城市:1095年12月-“ 3971883”,“美国”,14、752、15、931、949

City: 18 sur 1095 - "34", "United States", 052235, -118, 243683 , Chicago, 2720546 城市:1095年10月18日-“ 34”,“美国”,052235,-118、243683,芝加哥,2720546

I used the code from Wiktor Stribiżew 我使用了WiktorStribiżew的代码

/[^\\w\\s,-]/gi – / [^ \\ w \\ s,-] / gi –

And added a "." 并添加了一个“。” in it, as that was causing arrays to be separated 在里面,因为那导致数组分离

Moreover, I found that the person that has sent me the CSV changed its structure... so now I think it works, here's the final result. 而且,我发现向我发送CSV的人改变了结构...所以现在我认为它可以正常工作,这是最终结果。 Thanks for the help 谢谢您的帮助

var cities = await data.replace(/(?:\\[rn]|[\\r\\n]+)+/g, ',').replace(/[^\\w\\s,-.]/gi, ',').split(','); var city =等待数据。replace(/(?:\\ [rn] | [\\ r \\ n] +)+ / g,',')。replace(/ [^ \\ w \\ s,-。] / gi, ',')。分裂(',');

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

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