regex - Turning txt files into rows on a CSV using python -


I have a text file that has the same common name:

  2014521 RNC VATX Rex Obama Tatas 2012517 Update for Obama on 2014520 W H / Obama Weight Rollout. Txt  

then question the letters and / or letters after that. In every text file, there are words that I am trying to write a script that will take the first string of digits and add a CSV to a column named "date". Then the letters and / or letters should be taken after the number and they should be put in a column called "title". And then it should take the text inside the file and add it to a column called "content". I liked it very much but not the whole cigar when I run the script down, then every date = -1 and title = -1 is not wrong to me?

  f = open ('RNC.csv', 'w') name = ['date', 'title', 'content'] dw = csv.DictWriter (f, names) dw . For Route, Dinem, Filenames. (For '.') For .writerow ({k: for name of k:}): for file name in filenames: if not filename.endswith ('.txt'): title = filename .find ( R '\ D *') Date = filename.find (r '^ \ d *') open_doc = open (root + '/' + file name, 'r') content = open_doc.read (). Rstrip () Open_doc.close () dw.writerow ({'date': date, 'title': title, 'content': content}) f.close ()  
P>

The problem is that given the status of substring s in filename.find (s) Is filename . It returns -1 when the substrings are not found.

You can use a regex to match instead:

  File name in filenames: m = re.match ("\ A (\ d + ) (. *) \ .txt \ z ", file name) if m: date = m.group (1) title = m.group (2) ..  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -