简体   繁体   English

无法通过 nodejs 中的 fs.readFile() 读取整个文件

[英]Cannot read whole file through fs.readFile() in nodejs

I am trying to read and process following file我正在尝试读取和处理以下文件

My code is as under:我的代码如下:

const fs = require('fs')

function test(fileName = 'TechCrunchcontinentalUSA.csv'){
    return new Promise((resolve, reject)=>{
        fs.readFile(fileName, 'utf-8', (err, data)=>{
            if (err) reject(err.message)
            resolve(data)
        })
    })
}

async function temp(){
    let data = await test()
    console.log(data)
}

temp()

The out put is just two lines (and that too with junk data) as under:输出只有两行(还有垃圾数据),如下所示:

international-liars-poker-association,International Liars Poker Association,24,other,St. Louis,MO,1-Nov-07,1250000,USD,s
grid-networks,Grid Networks,,web,Seattle,WA,20-May-08,10500000,USD,bd00,USD,a,bD,b0,USD,seedaD,a

I can not understand what is wrong.我不明白出了什么问题。 Is the above code the right way to read a file like this?上面的代码是读取这样的文件的正确方法吗? Or is there other better way to read such file fully?还是有其他更好的方法来完全读取此类文件? Please guide me.请指导我。

Nothing is wrong.没有什么是错的。 You're just reading a file as a buffer and expecting it to act as an array of lines.您只是将文件作为缓冲区读取并期望它充当行数组。

If you run hexdump on the file you'll see the line breaks are all carriage returns ( '\r' or 0x0d).如果您在文件上运行hexdump ,您会看到换行符都是回车符( '\r'或 0x0d)。 There are no linefeeds.没有换行符。 When you dump the file, each carriage return causes the output to restart at the first column, without creating a new line.当您转储文件时,每个回车都会导致 output 在第一列重新启动,而不创建新行。

0000000 6570 6d72 6c61 6e69 2c6b 6f63 706d 6e61
0000010 2c79 756e 456d 706d 2c73 6163 6574 6f67
0000020 7972 632c 7469 2c79 7473 7461 2c65 7566
0000030 646e 6465 6144 6574 722c 6961 6573 4164
0000040 746d 722c 6961 6573 4364 7275 6572 636e
0000050 2c79 6f72 6e75 0d64 696c 6566 6f6c 6b63
                       ^^ carriage return
0000060 4c2c 6669 4c65 636f 2c6b 772c 6265 542c

When I cat the file on Linux, it all prints on one line.当我将文件放在 Linux 上时,它全部打印在一行上。 You're probably seeing two lines because one of the lines was long enough to trigger an auto-newline in your terminal.您可能会看到两行,因为其中一行的长度足以触发终端中的自动换行符。 (Additional evidence: The "International Liars Poker Association" line is in fact the longest line in the file.) (补充证据:“国际骗子扑克协会”这一行实际上是文件中最长的一行。)

If you split your buffer into lines using the '\r' as the delimiter and then print all the lines separately, you should get the output you expect.如果您使用'\r'作为分隔符将缓冲区拆分为行,然后分别打印所有行,您应该得到您期望的 output。

async function temp(){
    let data = await test()
    let lines = data.split('\r')
    lines.forEach(line => console.log(line))
}

nothing wrong with your code at first, it also didn't work in my local machine:一开始你的代码没有问题,它在我的本地机器上也没有工作:

my first assumption: something was wrong with the file,我的第一个假设:文件有问题,

I created a git repository and added the CSV to it.我创建了一个 git 存储库并将 CSV 添加到其中。 I opened and saved the file with my editor, which changes Windows-based text to Unix,我用我的编辑器打开并保存了文件,它将基于 Windows 的文本更改为 Unix,

在此处输入图像描述

and after that, it worked:之后,它起作用了:

在此处输入图像描述

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

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