473,378 Members | 1,360 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,378 software developers and data experts.

How to compare and change contents of text files

I need some basic lines of code that can do the following:

open a .con file (it's basically a txt file, just a different extension)

Then this .con file is full of lines like these

Expand|Select|Wrap|Line Numbers
  1. run /objects/staticobjects/pr/misc/shelving_v1/shelving_v1.con
  2.  
obviously you can see there's a path there, what the program needs to do now is check for a
Expand|Select|Wrap|Line Numbers
  1. shelving_v1_nav.con
file under the path
Expand|Select|Wrap|Line Numbers
  1. objects/staticobjects/pr_edit/_stuff/shelving_v1_nav/
and if a
Expand|Select|Wrap|Line Numbers
  1. shelving_v1_nav.con
exists there,

then take the line (in the txt)
Expand|Select|Wrap|Line Numbers
  1. run /objects/staticobjects/pr/misc/shelving_v1/shelving_v1.con
  2.  
and change it to

Expand|Select|Wrap|Line Numbers
  1. run /objects/staticobjects/pr_edit/stuff/shelving_v1_nav/shelving_v1_nav.con
  2.  
the thing is, i can't put the filenames in the program, it needs to cross check those files everytime it's run, as these file names change and are added to all the time.

I hope you understand what I'm trying to explain here :)
May 22 '10 #1
3 2132
Glenton
391 Expert 256MB
@creepinshadow
Hi

This doesn't sound terribly difficult, but I did get a bit lost towards the end, I must admit!

A couple of points.
1. The open command is what you're looking for. This comes with different modes, read only (r), write (w), and append (a), as well as an updating mode (r+). I've never actually used this last, but it might be worth exploring, as it's possibly what you're after. However, I would tend to use two separate files, and definitely for development this is a good idea!
2. The rules about what the change is that's required need to be clear. They aren't from your post! We'll suppose for the sake of the pseudocode below that you write a function that converts the line to the new line:
Expand|Select|Wrap|Line Numbers
  1. def myUpdater(line):
  2.     #take line and apply the update rules
  3.     return newline
3. The os module, and the path and walk commands may also be useful to you for going through all the files in a directory and/or subdirectories in turn. Very handy and works intuitively (like most of python!)



But roughly this kind of code:

Expand|Select|Wrap|Line Numbers
  1. myReadFile = open("whatever.con","r")
  2. myWriteFile = ("whatever.con.temp","w")
  3. for line in myReadFile:
  4.     if 'shelving_v1_nav.con' in line:
  5.         #do whatever rule is required to edit the path in line
  6.         #write this new line to myWriteFile
  7.         myWriteFile.write(myUpdater(line))
  8.     else:
  9.         myWriteFile.write(line)
  10. myReadFile.close()
  11. myWriteFile.close()
How you write myUpdater depends on the complexity of the rules really, and the regularity of the input data. You might want to check out regular expressions - bit of a learning curve, but very powerful in the right circumstances. Which your situation may, or may not be.

Good luck!
May 23 '10 #2
thank you so much!

i think i'll be able to do a lot with just a little starting help!

this will make things soo much easier!!
May 24 '10 #3
Glenton
391 Expert 256MB
No problem. Let us know how it goes.
May 25 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ray Slakinski | last post by:
I got a small issue, I am using urllib.urlretreive to download files but in some cases I'm downloading from a CGI that is redirecting urlretrieve to a different url. Example: ...
2
by: sudheervemana | last post by:
Dear all, In my main directory there are some source files and i have another directory which includes several folders,each contains the make files.Now i want to debug my source code in either...
1
by: TK | last post by:
I have a file upload control to allow a user to select/upload a file to the server. They need to upload x number of files in one shot because they have to confirm that they are uploading x number...
8
by: crystalattice | last post by:
I'm sure this has been addressed before but it's difficult to search through several thousand postings for exactly what I need, so I apologize if this a redundant question. I've figured out how...
2
by: stev1e | last post by:
I am new to Perl scripting but I need to compare a list of users in one file to a list of users in another file e.g File A File B B. Simpson ...
26
by: CodeTilYaDrop | last post by:
Does anyone know how to compare contents of two ArrayLists? I want to compare them to see if they match up. Do you have an example or could you point me in the right direction. I know I need to...
1
by: raj85 | last post by:
i want to compare 2 same files which is located at different location. i olready created 2 file browser button. now i juz need c# code to do comparing text of the both files. it muct check both...
4
by: Miner Jeff | last post by:
Hello, I have a basic question about reading files. I have several data files where the filenames are identical except for a short (3 character) prefix. I inherited this code and the person who...
12
by: lilly07 | last post by:
Hi I have two text files and each file contains 2 tab separated strings as below: File R.txt class1 12345 class2 26789 class1 4567
0
by: Arulmanoj | last post by:
Hi, I need a batch file to delete all PDF files in a folder and it's subfolders. I will be giving the folder path and number of old days to delete as static values.. Please it's very urgent. Also...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.