Solved

Go over a ltf file with python

Posted on 2011-02-20
4
380 Views
Last Modified: 2012-05-11
Dear experts,
I am a newbie in the python world. I would like to build a small script that goes over a specific file (UTF-8) and do the following:
For each line that start with XXXX copy the line, remove the XXXX and put the output on a different file.
So eventually I will have a new file with all the lines from the original file that had XXX (without the XXX).
Can you please provide me some example that similar to what I would like to build?
Best regards,
Boaz.
0
Comment
Question by:WAS_Infra
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Bxoz
ID: 34936232
# -*- coding: iso-8859-1 -*-
import re
obFile = open('fileToRead.txt','r')
obFileW = open('fileToWrite.txt','w')

lignes = obFile.readlines()
reg1=re.compile('^XXX')
for i in lignes:
    if reg1.findall(i):
        obFileW.write(i)
obFile.close()
obFileW.close()

Open in new window

0
 
LVL 6

Accepted Solution

by:
Bxoz earned 500 total points
ID: 34936264
Same code but with XXX removed

# -*- coding: iso-8859-1 -*-
import re
obFile = open('fileToRead.txt','r')
obFileW = open('fileToWrite.txt','w')

lignes = obFile.readlines()
reg1=re.compile('^XXX')
for i in lignes:
    if reg1.findall(i):
        obFileW.write(i.replace('XXX',''))
obFile.close()
obFileW.close()

Open in new window

0
 
LVL 28

Expert Comment

by:pepr
ID: 34940704
The solution with regular expressions is an overkill if you really do not need them.  For the case when the line starts with known prefix, use the .startswith() method of the built-in string.  Also, there is no need to read the lines first to a list.  It is better to process the file on-the-fly (the data.txt file attached):

fin = open('data.txt')
fout = open('out.txt', 'w')

for line in fin:
    if line.startswith('XXXX'):
        fout.write(line)
    
fin.close()
fout.close()

Open in new window

data.txt
0
 
LVL 28

Expert Comment

by:pepr
ID: 34940768
For the UTF-8, it is a separate story. It depends also on whether you use Python 2.x or Python 3.  If using Python 2.x it depends on whether you want to theat the strings as sequences of bytes or as unicode strings.  Use the codecs module http://docs.python.org/library/codecs.html for the later.  The difference with opening the files seems only a minor one...

import codecs

fin = codecs.open('data.txt', encoding='utf-8')
fout = codecs.open('out.txt', 'w', encoding='utf-8')

for line in fin:
    if line.startswith('XXXX'):
        fout.write(line)
    
fin.close()
fout.close()

Open in new window


However, the line variable now contains unicode strings.  You can even convert the encoding for the output.

I do recommend to read the "Dive into Python 3" by Mark Pilgrim, Chapter 4. Strings -- http://diveintopython3.org/strings.html
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question