473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Editing particular lines of a text file.

Hello all,

I am trying to create a script that looks at specific strings in a
file like:

msgid "I am a disco dancer."

and compares the part in quotes to the keys in a dictionary, finds the
value and creates a new line right after this string in the file. I
have planned to write this as follows:

1. Open the file in read mode
2. parse each line to figure out which line contains "msgid" and use
the shlex module's split method to go and split this line and pick the
2nd element list[1].
3. find the value from the dictionary corresponding to the above
element.
4. Insert the line. This part is where I face a problem. How do I
plainly edit just one line. I would also like to look at some sample
code that does this.
5. open a new file and write the new file with the inserted strings to
it.
6. close both files opened.

Regards,
Shriphani Palakodety

Oct 9 '07 #1
2 1201
On 09/10/2007, Shriphani <sh********@gmail.comwrote:
Hello all,

I am trying to create a script that looks at specific strings in a
file like:

msgid "I am a disco dancer."

and compares the part in quotes to the keys in a dictionary, finds the
value and creates a new line right after this string in the file. I
have planned to write this as follows:

1. Open the file in read mode
2. parse each line to figure out which line contains "msgid" and use
the shlex module's split method to go and split this line and pick the
2nd element list[1].
3. find the value from the dictionary corresponding to the above
element.
4. Insert the line. This part is where I face a problem. How do I
plainly edit just one line. I would also like to look at some sample
code that does this.
5. open a new file and write the new file with the inserted strings to
it.
6. close both files opened.

infile = open('infile.txt')
outfile = open('outfile.txt','w')
for line in infile:
if 'msgid' in line:
# transform line
# make sure the line ending is intact
outfile.write(line)
infile.close()
outfile.close()

or maybe

infile = open('infile.txt')
outfile = open('outfile.txt','w')
new_file = []
for line in infile:
if 'msgid' in line:
# transform line
# make sure the line ending is intact
new_file.append(line)
outfile.write(''.join(new_file)
infile.close()
outfile.close()


--

Tim Williams
Oct 9 '07 #2
Shriphani wrote:
Hello all,

I am trying to create a script that looks at specific strings in a
file like:

msgid "I am a disco dancer."

and compares the part in quotes to the keys in a dictionary, finds the
value and creates a new line right after this string in the file. I
have planned to write this as follows:

1. Open the file in read mode
2. parse each line to figure out which line contains "msgid" and use
the shlex module's split method to go and split this line and pick the
2nd element list[1].
3. find the value from the dictionary corresponding to the above
element.
4. Insert the line. This part is where I face a problem. How do I
plainly edit just one line. I would also like to look at some sample
code that does this.
5. open a new file and write the new file with the inserted strings to
it.
6. close both files opened.

Regards,
Shriphani Palakodety
Sounds like homework, but I'm feeling generous (not tested).

xlate={'"I am a disco dancer."':'"but John Travolta is better"'}
fp1=open('inputfile.txt', 'r')
fp2=open('outputfile.txt', 'w')

for line in fp1:
#
# Your description is unclear here about whether the new line
# replaces the existing one or is inserted after it.
#
fp2.writeline(line)
if line.startswith('msgid'):
parts=line.split(' ')
try: parts[1]=xlate[parts[1]]
except:
#
# Handle exception if your translation dictionary does
# not have the string you are looking for here.
#
raise KeyError

newline=' '.join(parts)
fp2.writeline(newline)

fp1.close()
fp2.close()
-Larry
Oct 9 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sam.collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8"...
1
by: pratyush.aditya | last post by:
hi, can anybody tell me what are the steps and procedures that are to be taken for video editing like frame extraction etc from avi,mpeg files using C programming.
4
by: ALF | last post by:
What's easiest way to replace a particular text string in a text file by another and save the modified file? Currently I know how to open a file, read the text string, place it in a variable,...
2
by: Jon | last post by:
I am trying to accomplish the following with a text file... 1 open the file 2 loop through it line by line 3 if need be, modify the current line 4 replace the old line in the file with the...
8
by: D | last post by:
Hi, I currently have a Python app with a Tkinter GUI frontend that I use for system administration. Everytime it launches, it reads a text file which contains info about each host I wish to...
2
by: ritesh | last post by:
Hi, I'm facing a problem in which I need to edit an already created file, and the editing needs to be done at the start of the file rather then appending to the file. OS - Linux,Solaris ...
3
by: Clarisa | last post by:
Hello Folks I am working on extracting lines of data in a text file based on the string it contains. This is the text file called info.txt:
1
ganesanji
by: ganesanji | last post by:
hi to all, I am new to php. I have to edit a text file using php. I saw the file system concepts modes. My problem is I want to change a particular text or word in a text file. How to...
10
by: bluemountain | last post by:
Hi there, Iam new to python forms and programming too I had a text file where i need to extract few words of data from the header(which is of 3 lines) and search for the keyword TEXT1, TEXT2,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.