473,320 Members | 2,027 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,320 software developers and data experts.

Trying to manipulate a list of words in a textfile

3
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 1544
isakb
3
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 Expert Mod 2GB
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
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 Expert Mod 2GB
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
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...
7
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
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...
7
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...
0
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...
1
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...
1
by: antar2 | last post by:
Hello Suppose I have a textfile (text1.txt) with following four words: Apple balcony cartridge damned paper
1
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.