首页 文章

扫描txt文件,保持字符之间的空白区域

提问于
浏览
0

我正在学习r中的情感分析,

我遇到了如何正确读取数据并将数据导入我的案例,

我想知道如何使用r中的scan()读取数据,
但是如何使它在从txt的一行中的字符串之间也读取空白区域

案件 :

例如,

positive.txt 包含:

Not Strong

*上面的字符串写在txt文件中没有分隔的半冒号或逗号,只是行间距\ n


所以,我在r中使用这个读取txt格式的文件:

positive - > scan('positive.txt',what ='',sep =“\ n”)

没有错误,

但经过我的检查,
主要问题是通过使用scan()读取导入的文件将读取字符之间的空白区域,
所以从输入 positive.txt 文件:

1) Not Strong

使用scan()到r中的 positive.txt 文件的结果将最终如下:
1) Not
2) Strong

我的期望是:
如何在r中保持scan()文件保持 positive.txt 内字符的空格:
1) Not Strong

我预期的结果仍然是:
1) Not Strong

*注意,数字1只是一些指南,如某种索引,以帮助理解这个问题 .

1 回答

  • 0
    $ cat /tmp/file.txt
    this is line 1
    this is line 2
    this is line 3
    

    在R:

    file.contents <- readLines('/tmp/file.txt')
    
    file.contents
    
    # [1] "this is line 1" "this is line 2" "this is line 3"
    

    运气情绪分析祝你好运,这是一个很棒的话题!

相关问题