Python 3.4:编辑txt文件中的所有行

时间:2015-06-11 05:29:48

标签: python-3.x

我的文字包含以下信息。

1000045|NICHOLAS FINANCIAL INC|SC 13D/A|2015-04-24|edgar/data/1000045/0000897069-15-000319.txt
1000045|NICHOLAS FINANCIAL INC|SC 13G/A|2015-05-29|edgar/data/1000045/0001193125-15-206657.txt
1000045|NICHOLAS FINANCIAL INC|SC 13G|2015-05-08|edgar/data/1000045/0001144204-15-028845.txt
1000097|KINGDON CAPITAL MANAGEMENT, L.L.C.|13F-HR|2015-05-15|edgar/data/1000097/0000919574-15-004506.txt
1000097|KINGDON CAPITAL MANAGEMENT, L.L.C.|SC 13G/A|2015-05-28|edgar/data/1000097/0000919574-15-004670.txt
1000097|KINGDON CAPITAL MANAGEMENT, L.L.C.|SC 13G/A|2015-06-08|edgar/data/1000097/0000919574-15-004791.txt
1000097|KINGDON CAPITAL MANAGEMENT, L.L.C.|SC 13G|2015-05-26|edgar/data/1000097/0000919574-15-004632.txt
1000097|KINGDON CAPITAL MANAGEMENT, L.L.C.|SC 13G|2015-06-08|edgar/data/1000097/0000919574-15-004793.txt
1000152|WESTERN INTERNATIONAL SECURITIES, INC.|X-17A-5|2015-04-16|edgar/data/1000152/9999999997-15-008305.txt
1000177|NORDIC AMERICAN TANKERS Ltd|20-F/A|2015-04-29|edgar/data/1000177/0000919574-15-003778.txt
1000177|NORDIC AMERICAN TANKERS Ltd|6-K|2015-04-30|edgar/data/1000177/0000919574-15-003819.txt
1000180|SANDISK CORP|10-Q|2015-04-30|edgar/data/1000180/0001000180-15-000027.txt
1000180|SANDISK CORP|4|2015-05-27|edgar/data/1000180/0001242648-15-000023.txt
1000180|SANDISK CORP|8-K|2015-04-15|edgar/data/1000180/0001000180-15-000023.txt
1000180|SANDISK CORP|ARS|2015-05-04|edgar/data/1000180/9999999997-15-009621.txt
1000180|SANDISK CORP|DEF 14A|2015-04-27|edgar/data/1000180/0001047469-15-003972.txt

我需要编辑信息并使用以下格式返回每一行,并在“edgar”之前删除。

edgar/data/1000180/0001047469-15-003972.txt

3 个答案:

答案 0 :(得分:0)

这是一种不导入任何库的方法:

假设此数据存储在名为lines = [] with open('data.txt') as data: for line in data: lines.append(line.rstrip()) # rstrip() removes the newline character for i in range(len(lines)): lines[i] = 'edgar' + lines[i].split('edgar')[1] 的文件中:

setwd()

答案 1 :(得分:0)

这是另一个选项(Python 2.x或3.x):

results = []

with open('file.txt') as f:
    for line in f:
        results.append(line.rstrip().split('|')[4]) # append to results the 4th element of the line
print(results)

这将循环遍历文件,在管道字符上拆分,然后取第4个元素(你想要的部分)。

它假定在名为file.txt的输入文件中。

答案 2 :(得分:0)

这最终为我工作。

with open('file.txt') as f:
    data = str.splitlines(f.read())
    for eachLine in data:
        print(eachLine.split('|')[-1])