473,574 Members | 17,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to manipulate a list of words in a textfile

3 New Member
Hi,

I am quite new to Python, and the following puzzles me, maybe there is someone here who can tell me what is wrong?

I have a list of words in a plain textfile (encoded in latin-1). There is one word per line, and 106299 words in total. I want to load the words to a list because I want to manipulate them, and then write them to a different file...

The mystery for me is that the new file only contains 106211 lines, even though I have not done anything with the list yet, and the last line contains a word that has been truncated.

I have tried many different version of the program, but this is the most basic one, and it gives the error mentioned above:

Expand|Select|Wrap|Line Numbers
  1. f = open("list.txt", "r")
  2. wordlist = [w.lower() for w in f.readlines()]
  3. f.close
  4.  
  5. f = open("new_list.txt", "w")
  6. f.writelines(wordlist)
  7. f.close
  8.  
I am running this from Windows XP in Python 2.5.1, using IDLE.
Jan 2 '08 #1
4 1556
isakb
3 New Member
An update: I was just told that IDLE is basically "useless" by a fellow programmer, so I tried running the script directly from the terminal instead of using IDLE. So this seems to be just an IDLE bug? (Sorry for not realizing that the results would differ, I thought IDLE used the same python as the terminal.)
Jan 2 '08 #2
bvdet
2,851 Recognized Expert Moderator Specialist
An update: I was just told that IDLE is basically "useless" by a fellow programmer, so I tried running the script directly from the terminal instead of using IDLE. So this seems to be just an IDLE bug? (Sorry for not realizing that the results would differ, I thought IDLE used the same python as the terminal.)
The problem is not with IDLE. You did not close the file. You must include '()' when closing the file.
Expand|Select|Wrap|Line Numbers
  1. f = open('file_name')
  2. f.close()
The data is flushed through the buffer when the file is close. All the data was not written in your case.
Jan 2 '08 #3
isakb
3 New Member
Oh, thank you for that information. I also tried changing the buffering mode to none, which made it work in IDLE, so your explanation makes much sense. So I understand that you should always close a file after writing to it, but it is not necessary when reading from it?
Jan 2 '08 #4
bvdet
2,851 Recognized Expert Moderator Specialist
Oh, thank you for that information. I also tried changing the buffering mode to none, which made it work in IDLE, so your explanation makes much sense. So I understand that you should always close a file after writing to it, but it is not necessary when reading from it?
It is always a good idea to close every file that you open. Python takes care of garbage collection, so it is not a critical issue when reading the contents of a file as in:
Expand|Select|Wrap|Line Numbers
  1. data = open('file_name').read()
In this case, no file object is created.
Jan 2 '08 #5

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

Similar topics

2
1839
by: Warren Oates | last post by:
If I have a textfile of words, each word on one line: dog cat viagra for instance, and I get them into an array, how then can I replace those words in a string with something else: "The quick dog took viagra and the lazy cat was unhappy."
7
2957
by: Klaus Neuner | last post by:
Hello, I need a function that converts a list into a set of regexes. Like so: string_list = print string_list2regexes(string_list) This should return something like:
3
15609
by: Stewart | last post by:
Dear comp.lang.javascript, I have more than once wanted to manipulate the contents of select boxes dynamically, whilst the boxes contain <optgroup> tags. Manipulation of a select box containing only <option> tags is fairly easy and there is plenty of material on the net to assist with this. However, I have searched long and found the...
7
2351
by: 3KWA | last post by:
Hi all, I tried to send a small mailing list using python today (2036 emails). For that purpose I looked in the doc for some code examples (I had never done it before). And I ended up writing and running this little script: # list.txt is a text file container an email per line fp=open('list.txt','r') list=fp.readlines()
0
1176
by: gg302 | last post by:
Hey, i have a textfile hosted on a website which is a huge list (over 2000 lines). I need to get some data from it but it would take forever for me to manually remove the characthers i dont want, so i tried to use Inet and split the data then place the words i want into a textbox. I only need the words that are between "furni_" and "_name" On...
1
1164
by: =?Utf-8?B?UmFm?= | last post by:
Hello, I have to write an application that scans textfiles for certain words. I'm talking about approximately 5000 words. The only way I can think of to do this is to scan each textfile for each word. This takes a lot of time and a lot of capacity of my pc. Is there another way to do this task? Regards, Raf
1
1224
by: antar2 | last post by:
Hello Suppose I have a textfile (text1.txt) with following four words: Apple balcony cartridge damned paper
1
2806
by: asedt | last post by:
With my Excel macro and two text files I want to create a new textfile containing the first textfile then text from the sheet and then the second textfile. My problem is that i don't know how to append the second textfile. Sub mysub() Dim TempString As String 'Open the first file Dim fileA As Integer
0
7820
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...
0
7738
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...
0
8081
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. ...
0
8258
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...
0
6486
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5635
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5325
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...
1
2255
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1360
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.