473,663 Members | 2,877 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 1561
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
1840
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
2958
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
15610
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 material on the subject of <optgroup> to be sparse, with a few posts here & there, but basically you're...
7
2357
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
1177
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 the list it looks like this "Furni_*CODE_I_NEED*_name" this is what i have tried Private Sub...
1
1166
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
1227
by: antar2 | last post by:
Hello Suppose I have a textfile (text1.txt) with following four words: Apple balcony cartridge damned paper
1
2809
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
8436
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
8345
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
8858
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
7371
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5657
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
4182
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
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
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
2
2000
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.