473,840 Members | 1,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I cut parts from a text file

Hi,

I am new to Python and need to parse a text file and cut parts out i.e. say
the text file contained 5 rows of text:

line 1 of the text file
line 2 of the text file
line 3 of the text file
line 4 of the text file
line 5 of the text file

And the text I want starts at line 2 and goes through to line 4, what is the
best way to cut this text out? I guess I need to mark the start and end of
the text and then pass these values to a command which will chop the file
at that location however I am at a loss.

Any advice or pointers of where to find more info would be greatly
appreciated.

Thanks

RiGGa
Jul 18 '05 #1
1 9424
"Rigga" <ri***@hasnomai l.com> wrote:
I am new to Python and need to parse a text file and cut parts out i.e. say
the text file contained 5 rows of text:

line 1 of the text file
line 2 of the text file
line 3 of the text file
line 4 of the text file
line 5 of the text file

And the text I want starts at line 2 and goes through to line 4, what is the
best way to cut this text out?


the same way as you'd do it in any other programming language...

you can add stuff to the end of a file, but you cannot insert or remove stuff
inside the file, without rewriting the rest of the file. unless your files are really
huge, the easiest way to do this is to

1) load the entire file into memory

text = open(filename). readlines()

2) manipulate the data structure in memory

# remove first and last line
del text[0], text[-1]

3) write it back

f = open(outfilenam e, "w")
f.writelines(te xt)
f.close()

</F>


Jul 18 '05 #2

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

Similar topics

4
2151
by: Stuk | last post by:
Hi, im false beginner in C so that`s why im writting here :). I have to write a Text Reformater, which should read data from text file every Verse. In text file may appear special directives (for example .xx n where n is a variable). Every directive, cause something else (set marigin, space between verses, lenght of page, header, etc.) I don't know how to start ? which data structure should I choose ? Thanks for possible anserws
2
1322
by: The Bear | last post by:
After a dataset has been populated from a text file, is it possible to update a text file from a dataset once changes are made? TB
5
3927
by: Patrick Sannes | last post by:
Hello, I want to read an 1GB text file in parts of 100 lines. When I use just a simple stram/filereader, I think it reads the whole file, wich results in a hanging machine. Is there a way to do it in blocks? For debugging this is a lot easyer. Patrick
3
1403
by: Kou Vang | last post by:
I have a large text file seperated by commas. I was wondering how to get the values from the text file. The problem is that not all the characters are the same length, so I can't use the characters, but is I can't find how to seperate out the values by the commas. I know that reading the whole line is possible, but what if I just want the third value and not the rest? Thanks. exp: 9821,1550,200,2004
14
17054
by: Niron kag | last post by:
Hello ! With c# , I want to write to a text file in a specific font and color. Any ideas ? Thanks...
29
4916
by: list | last post by:
Hi folks, I am new to Googlegroups. I asked my questions at other forums, since now. I have an important question: I have to check files if they are binary(.bmp, .avi, .jpg) or text(.txt, .cpp, .h, .php, .html). How to check a file an find out if the file is binary or text? Thanks for your help.
13
1582
by: kronecker | last post by:
I am trying to delete multiple lines in a text file using the following Private Sub Read_TextFile() Dim objReader As StreamReader Dim strfull, strContents, strContentsold, strContentsnew As String objReader = New StreamReader("C:\answer.txt") 'Clear the Text Box1 TextBox1.Clear()
8
18838
by: iheartvba | last post by:
Hi I am using Access 2007 and am trying to export a query to a fixed length text file. I tried using the following code to export the text file: DoCmd.TransferText acExportFixed, , "qryFFRDeFile", "C:\qryFFRDeFile.txt", True Access asks for a specification name, now here lies the challenge, how do I create an export specification I have tried the following 2 methods: In access 2007: Step1: I right clicked the query and chose export to...
0
1394
tuxalot
by: tuxalot | last post by:
I have a text file showing files in directories. The text file looks like this: "C:\folderA\file1.zip" "C:\folderA\folderB\folderC\file2.zip" "C:\folderA\folderE\folderX\folderU\file3.zip" The text file is created by using a batch file with this code segment: for /r %inpath% %%g in (*.zip) do echo "%%g" >> textfile.txt Now, I wish to use the same directory tree and build a second text file with a list of the zip file names contained...
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10916
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10598
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.