【发布时间】:2014-05-27 02:38:45
【问题描述】:
我有一批原始文本文件。每个文件都以Date>>month.day year News garbage开头。
garbage 是一大堆我不需要的文本,而且长度各不相同。 Date>> 和 News 这两个词总是出现在同一个地方,不会改变。
我想复制月日年并将此数据插入到 CSV 文件中,每个文件都有一个新行,格式为 日月年。
如何将月日年复制到单独的变量中?
我尝试在已知单词之后和已知单词之前拆分字符串。我对 string[x:y] 很熟悉,但我基本上想将 x 和 y 从数字更改为实际单词(即 string[Date>>:News])
import re, os, sys, fnmatch, csv
folder = raw_input('Drag and drop the folder > ')
for filename in os.listdir(folder):
# First, avoid system files
if filename.startswith("."):
pass
else:
# Tell the script the file is in this directory and can be written
file = open(folder+'/'+filename, "r+")
filecontents = file.read()
thestring = str(filecontents)
print thestring[9:20]
一个示例文本文件:
Date>>January 2. 2012 News 122
5 different news agencies have reported the story of a man washing his dog.
【问题讨论】: