Using fileinput.input ('test.txt') I probably forgot to process all
lines or so, since I get the error 'input() already active' when i try
to call fileinput.input ('test.txt') again. But how can I 'close' the
previous version I opened? I have no handle to it or so... 6 8732
cyberco wrote:
Using fileinput.input ('test.txt') I probably forgot to process all
lines or so, since I get the error 'input() already active' when i try
to call fileinput.input ('test.txt') again. But how can I 'close' the
previous version I opened? I have no handle to it or so...
fileinput.close ()
Or you forego the global FileInput instance with
fi = fileinput.FileI nput(...)
for line in fi:
# process line
fi.close()
Personally, I use ordinary files instead.
Peter
Ah, thanks!
Another related question I have: The following piece edits in place,
but ads whitelines between all lines of a Windows text file. Why?
=============== ============
fi = fileinput.input ('test.txt', inplace=1)
for l in fi:
print l.replace('a', 'b')
=============== ============
cyberco wrote:
Another related question I have: The following piece edits in place,
but ads whitelines between all lines of a Windows text file. Why?
=============== ============
fi = fileinput.input ('test.txt', inplace=1)
for l in fi:
print l.replace('a', 'b')
=============== ============
http://docs.python.org/lib/module-fileinput.html
/.../ lines are returned including the trailing newline when it
is present.
and "print" adds its own newline, of course. try
print text,
or
sys.stdout.writ e(text)
</F>
cyberco wrote:
Ah, thanks!
Another related question I have: The following piece edits in place,
but ads whitelines between all lines of a Windows text file. Why?
=============== ============
fi = fileinput.input ('test.txt', inplace=1)
for l in fi:
Please don't use lowercase "L" as a variable name; it is too easily
confused with the digit 1 in some fonts. Use meaningful names.
print l.replace('a', 'b')
=============== ============
fi = fileinput.input ('test.txt', inplace=1)
for line in fi:
print line.replace('a ', 'b')
It's nothing to do with whether the file is a "Windows text file" or
not. The reason is that line already has a "\n" i.e. newline character
at the end. The print statement adds another.
Either:
(1) change
print blahblah
to
print blahblah, # comma suppresses the added newline
or
(2) put
import sys
up the front and change
print blahblah
to
sys.stdout.writ e(blahblah)
HTH,
John
Please don't use lowercase "L" as a variable name;
Ohoh...I plead guilty! I totally forgot about CS 101... :)
cyberco wrote:
Please don't use lowercase "L" as a variable name;
Ohoh...I plead guilty! I totally forgot about CS 101... :)
I don't understand the reference to CS 101.
It's quite simple: If you want people to bother reading your postings
and help you, make them easy to comprehend. Otherwise they won't bother
a second time. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: not 2 swift |
last post by:
I thought I would update an old page on which I had used a
<INPUT TYPE=image ...>
with a
<BUTTON><IMG SRC=...></BUTTON>
The problem is that <BUTTON> always provides a shadow/emboss effect.
Can I get rid of that? If the old <INPUT> button has been deprecated in
favor of <BUTTON>, have all it's button functions been duplicated?
Thanks for any help,
|
by: TR |
last post by:
Is it possible with CSS to prevent this wrapping alignment with a checkbox
with a nested label?
This is the label of the
checkbox that wraps beneath it
I'd prefer it looked like this, with a flush left margin:
|
by: Ben |
last post by:
Here's my form:
<form name="aForm" method='post'>
<input type=file name=file1 onkeypress='KeyPress()'><br>
<a
id='attachMoreLink'
href='javascript:AddFileInput()">Attach More Files
</a>
<input type=submit value='Done'>
</form>
|
by: Rick |
last post by:
I'm trying to make an input tag visible or hidden based on the value of
another input tag, so that when the 1st input tag is changed in anyway, the
2nd input tag will become visible.
Thanks in advance,
Rick
|
by: Steve Swift |
last post by:
If it is possible, could someone demonstrate how to make the text
entered into an <INPUT TYPE=TEXTfield right-align within the entry
field, please?
The fields in my form are for numbers, and they look strange aligned left.
--
Steve Swift
http://www.ringers.org.uk
| |
by: cyberco |
last post by:
Opening, reading and writing to a file works fine in mod_python, but
using fileinput (with inplace editing) gives me a 'permission denied'
with exactly the same fileName:
=========================
fileinput.input(fileName, inplace=1)
=========================
I suspect that this has to do with the temporary file it creates, or am
I wrong?
|
by: Sreenath Rao Nellutla |
last post by:
Hai all,
I am trying to create dropdown calendar control with HTML input control by writing JavaScript. But while executing I am getting the error as "Error on Page" on the status bar of the browser. I wrote the following code in the HTML code for the web form:
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="calendar.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>...
|
by: jp2code |
last post by:
I have several input fields on my form, and the form works; however, Visual
Studio is showing errors, and I would like to get rid of them.
When the form is submitted, it is redirected back to itself, where it checks
the values that were entered.
If the default values are set, this is the visitor's first time to access
the page.
Example: The FirstName field would initially be displayed with "First Name;"
|
by: JPhilS |
last post by:
Hi to all Webmaster!
I have discover this problem when I inserted the scores of the students i a centain subject.
I am making a school project with regards to saving students' record.
first, I have to view all students under the certain subject and at the last field, I putted a form to put the scores of each student.
say '<input type=text name='score' size=10>' and number of '<input type=text>' is equal to the number of students.
I...
|
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,...
|
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...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |