<br><br><div class="gmail_quote">On Sat, Oct 2, 2010 at 1:18 PM, Emile van Sebille <span dir="ltr">&lt;<a href="mailto:emile@fenx.com">emile@fenx.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
On 10/2/2010 8:56 AM Josep M. Fontana said...<div class="im"><br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hi,<br>
<br>
This is my first posting to this list. Perhaps this has a very easy answer<br>
but before deciding to post this message I consulted a bunch of Python<br>
manuals and on-line reference documents to no avail. I would be very<br>
grateful if someone could lend me a hand with this.<br>
<br>
</blockquote>
<br></div>
Hi Josep,<br>
<br>
Break the problem into pieces.<br>
<br>
Conceptually, you&#39;ll need to:<br>
<br>
  -a- get the list of file names to change then for each<br>
  -b- determine the new name<br>
  -c- rename the file<br>
<br>
For -a- you&#39;ll need glob. For -c- use os.rename.  -b- is a bit more involved.  To break -b- down:<br>
<br>
  -b1- break out the x-xx portion of the file name<br>
  -b2- look up the corresponding year in the other file<br>
  -b3- convert the year to the century-half structure<br>
  -b4- put the pieces together to form the new file name<br>
<br>
For -b2- I&#39;d suggest building a dictionary from your second files contents as a first step to facilitate the subsequent lookups.<br>
<br>
You haven&#39;t included any code and we can&#39;t gauge how far along you are, so follow up and post the code you write as you go and we&#39;ll be better able to help out.<br>
<br>
Hope this is enough to get you started,<br>
<br>
Emile<br>
<br></blockquote><div>I echo Emile&#39;s excellent description.  He didn&#39;t speak to changing what is in the file.  I would read from the first file a line at a time and concatinate the citation date information to that string, then write the string to a second file that is named according to your desired rule.  Then copy the rest of the text to the new file.  When you are done, save the new file and delete the original if you don&#39;t need it any longer.<br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
<br>
<br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div></div><div class="h5">
Here&#39;s the problem I want to solve. I have a lot of files with the following<br>
name structure:<br>
<br>
A-01-namex.txt<br>
A-02-namey.txt<br>
...<br>
N-09-namez.txt<br>
<br>
These are different text documents that I want to process for an NLP project<br>
I&#39;m starting. Each one of the texts belongs to a different century and it is<br>
important to be able to include the information about the century in the<br>
name of the file as well as inside the text.<br>
<br>
Then I have another text file containing information about the century each<br>
one of the texts was written. This document has the following structure:<br>
<br>
A-01, 1278<br>
A-02, 1501<br>
...<br>
N-09, 1384<br>
<br>
What I would like to do is to write a little script that would do the<br>
following:<br>
<br>
. Read each row of the text containing information about the centuries each<br>
one of the texts was written<br>
. Change the name of the file whose name starts with the code in the first<br>
column in the following way<br>
<br>
         A-01-namex.txt --&gt;  A-01-namex_13-2.txt<br>
<br>
     Where 13-1 means: 13th 2nd half. Obviously this information would com<br>
from the second column in the text: 1278 (the first two digits + 1 =<br>
century; if the 3rd and 4th digits&gt;  50, then 2; if&lt;  50 then     1)<br>
<br>
Then in the same script or in a new one, I would need to open each one of<br>
the texts and add information about the century they were written on the<br>
first line preceded by some symbol (e.g @13-2)<br>
<br>
I&#39;ve found a lot of information about changing file names (so I know that I<br>
should be importing the os module), but none of the examples that were cited<br>
involved getting the information for the file changing operation from the<br>
contents of a document.<br>
<br>
As you can imagine, I&#39;m pretty green in Python programming and I was hoping<br>
the learn by doing method would work.  I need to get on with this project,<br>
though, and I&#39;m kind of stuck. Any help you guys can give me will be very<br>
helpful.<br>
<br>
Josep M.<br>
<br>
<br>
<br>
<br></div></div>
_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</blockquote>
<br>
<br>
_______________________________________________<br>
Tutor maillist  -  <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Joel Goldstick<br><br>