标签: readlines

使用python的readlines时忽略last \n

我有一个我从中读取的文件,如下所示:

1   value1
2   value2
3   value3
Run Code Online (Sandbox Code Playgroud)

该文件在最后一行中可能有也可能没有尾随\n.

我正在使用的代码效果很好,但如果有一个尾随\n它失败了.
什么是抓住这个的最好方法?

我的代码供参考:

r=open(sys.argv[1], 'r');
for line in r.readlines():
    ref=line.split();
    print ref[0], ref[1]
Run Code Online (Sandbox Code Playgroud)

哪一个会失败的:
Traceback(最近一次调用最后一次):
文件"./test",第14行,在
print ref [0]中,ref [1]
IndexError:列表索引超出范围

python readlines

6
推荐指数
1
解决办法
1万
查看次数

计算单词频率并从中制作字典

我想从文本文件中获取每个单词,并在字典中计算单词频率.

例: 'this is the textfile, and it is used to take words and count'

d = {'this': 1, 'is': 2, 'the': 1, ...} 
Run Code Online (Sandbox Code Playgroud)

我不是那么远,但我只是看不出如何完成它.我的代码到目前为止:

import sys

argv = sys.argv[1]
data = open(argv)
words = data.read()
data.close()
wordfreq = {}
for i in words:
    #there should be a counter and somehow it must fill the dict.
Run Code Online (Sandbox Code Playgroud)

python dictionary count readlines

6
推荐指数
3
解决办法
5万
查看次数

如何使用R从一行中提取多个字符串

我想从一行中提取多个字符串.

假设我有以下文本行(使用'readLines'函数形成一个网站):

line <- "abc:city1-street1-long1-lat1,ldjad;skj//abc:city2-street2-long2-lat2,ldjad;skj//abc:city3-street3-long3-lat3,ldjad;skj//abc:city3-street3-long3-lat3,ldjad;skj//"
Run Code Online (Sandbox Code Playgroud)

我想提取以下内容分开:

[1] city1-street1-long1-lat1
[2] city2-street2-long2-lat2
[3] city3-street3-long3-lat3
[4] city4-street4-long4-lat4
Run Code Online (Sandbox Code Playgroud)

我希望有人可以给我一个如何执行此任务的提示.

regex text r readlines

5
推荐指数
2
解决办法
366
查看次数

在一行中返回正则表达式搜索的第二个实例

我有一个具有特定兴趣行(例如,第12行)的文件,如下所示:

conform: 244216 (packets) exceed: 267093 (packets)
Run Code Online (Sandbox Code Playgroud)

我编写了一个脚本来通过正则表达式提取第一个数字并将值转储到一个新文件中:

getexceeds = open("file1.txt", "r").readlines()[12]
output = re.search(r"\d+", getexceeds).group(0)

with open("file2.txt", "w") as outp:
    outp.write(output)
Run Code Online (Sandbox Code Playgroud)

我还不够好将该行中的第二个数字返回到一个新文件中 - 有人可以提出建议吗?

一如既往地感谢您的帮助!

python regex readlines

5
推荐指数
2
解决办法
6255
查看次数

在 R 中删除文本文件的行

我有一个 .xml 文件,我用 R 中的 readLines() 读取。我想知道是否有一些函数可以让我从第 15 行删除到第 18 行。

我需要一个通用命令,因为我必须在同一个 .xml 文件上循环重复该函数,但对于以下几行。

r readlines

5
推荐指数
1
解决办法
7786
查看次数

如何在函数式编程(F#)中将行号添加到文本文件中?

它适用于for循环和可变变量:

let addLnNum filename =    
    use outFile = new StreamWriter(@"out.txt")    
    let mutable count = 1
    for line in File.ReadLines(filename) do
        let newLine = addPre (count.ToString()) line
        outFile.WriteLine newLine
        count <- count + 1
Run Code Online (Sandbox Code Playgroud)

但它非常"无功能",所以我很好奇这样做的正确方法是什么?我想到如何将索引号附加到字符串列表:

let rec addIndex (startInd:int) l=
    match l with
    |x::xs ->  startInd.ToString()+x :: (addIndex (startInd+1) xs)
    |[] -> []
Run Code Online (Sandbox Code Playgroud)

但它不适用于File.ReadLines:

let addLnNum2 filename =    
    use outFile = new StreamWriter(@"out.txt")    
    File.ReadLines(filename)
    |> addIndex 1
    |> ignore
    //Error 1   Type mismatch. Expecting a Collections.Generic.IEnumerable<string> -> 'a    
    //but given a …
Run Code Online (Sandbox Code Playgroud)

f# seq readlines

5
推荐指数
1
解决办法
248
查看次数

如何阅读文本框中的最后一行?

我有一个不断更新的多行文本框.我只需要阅读文本框中的最后一个单词/句子.

string lastLine = textBox1.ReadLine.Last();
Run Code Online (Sandbox Code Playgroud)

c# linq string textbox readlines

5
推荐指数
1
解决办法
5038
查看次数

Python读取两个字符串之间的特定文本行

我无法让python读取特定的行.我正在做的是这样的事情:

lines of data not needed
lines of data not needed
lines of data not needed

--------------------------------------
    ***** REPORT 1 *****
--------------------------------------

[key] lines of interest are here
[key] lines of interest are here
[key] lines of interest are here
[key] lines of interest are here
[key] lines of interest are here      #This can also be the EOF

--------------------------------------    
    ***** REPORT 2 *****
--------------------------------------

lines of data not needed
lines of data not needed
lines of data not needed         #Or …
Run Code Online (Sandbox Code Playgroud)

python text readlines

4
推荐指数
1
解决办法
1万
查看次数

来自readlines()的Groovy正则表达式匹配列表

我试图读取一个文本文件并返回所有不以#开头的行.在python中我可以轻松使用列表理解列表

with open('file.txt') as f:
     lines = [l.strip('\n') for l in f.readlines() if not re.search(r"^#", l)]
Run Code Online (Sandbox Code Playgroud)

我想通过Groovy完成同样的事情.到目前为止,我有以下代码,非常感谢任何帮助.

lines = new File("file.txt").readLines().findAll({x -> x ==~ /^#/ })
Run Code Online (Sandbox Code Playgroud)

regex groovy readlines

4
推荐指数
1
解决办法
4644
查看次数

如何在R中使用readLines读取某个范围内的所有行?

我正在尝试将大型 JSONL(.gz) 文件拆分为多个 .csv 文件。我已经能够使用下面的代码为前 25.000 个条目创建一个有效的 .csv 文件。我现在想读取和解析 25.001 到第 50.000 行,但一直无法这样做。我觉得这应该很容易完成,但到目前为止我的搜索没有结果。

有没有办法操纵 readLines 函数中的“n”因子来选择特定范围的行?

(ps我正在学习;))

setwd("filename")

a<-list.files(pattern="(.*?).0.jsonl.gz")
a[1]

raw.data<- readLines(gzfile(a[1]), warn = "T",n=25000) 
rd <- fromJSON(paste("[",paste(raw.data,collapse=','),']'))
rd2<-do.call("cbind", rd) 

file=paste0(a,".csv.gz") 
write.csv.gz(rd2, file, na="", row.names=FALSE)
Run Code Online (Sandbox Code Playgroud)

r readlines jsonlite

4
推荐指数
1
解决办法
4635
查看次数

标签 统计

readlines ×10

python ×4

r ×3

regex ×3

text ×2

c# ×1

count ×1

dictionary ×1

f# ×1

groovy ×1

jsonlite ×1

linq ×1

seq ×1

string ×1

textbox ×1