473,668 Members | 2,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Newbie question: casting problem

1 New Member
Hello I am starting to use Python.
I found a nice little example for subsituting and splitting texts and counting words separated by defined expressions (in the example , and blanks). Unfotunately there is an error in this example regarding the casting of strings: (see the code below):


#zerleg.py
import re
try:
fh=open('input. txt','r')
summe=0
n=0
while 1:
Zeile = fh.readline()
if Zeile =='':break
mZeile = re.sub('[ ,\r\n]{1,}$','',Zeile )
Wert = re.split('[ ,]{1,}',mZeile)
for i in range(len(Wert) ):
print'Wert:', Wert[i], '\n', len(Wert)
summe = summe + float(Wert[i])
n=n+len(Wert)
print"Summe: ", summe, \
'\nEingabedaten : ', n, \
'\nMittelwert : ', summe/n, '\n'
fh.close
except IOError, (errno, strerror):
print'Datei input.txt nicht lesbar. \n', \
'Fehlernummer:' , errno, '\n', \
'Fehlermeldung: ', strerror

Using the example above I am getting the following error message:

--> invalid literal for float(): ... fisrt word in file input.txt

The content of my input.txt is:
ba,nd001
hugo HiHo,002
alter ego 009
ha, what 006
003
END !? NO 010

Any idea how this casting problem could be solved?


Bye Guncrew
Mar 12 '08 #1
1 1172
jlm699
314 Contributor
Expand|Select|Wrap|Line Numbers
  1.         summe = summe + float(Wert[i])
You cannot float cast any characters. .. are you trying to count the number of words? If so, just simply use:
Expand|Select|Wrap|Line Numbers
  1. summe += 1
Mar 12 '08 #2

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

Similar topics

2
1784
by: Angelos Karantzalis | last post by:
Hi guys, I'm trying to load a class instance dynamically, and then cast it to it's base type so I can use it in my app. More specifically, I'm dynamically instantiating a System.Web.UI.WebControls.RequiredFieldValidator and attempting to cast it into an IValidator and/or a BaseValidator. Both casting attempts have failed miserably, throwing an InvalidCastException ... how can it be i cannot cast to the base class of my instance ???
28
10262
by: Yacine | last post by:
Does anyone know why void main(){ double x; double *y; (int) y = x; } compiles well, and why
26
2022
by: Method Man | last post by:
Say I have the following: int main(void) { char* p, q; p = (char*) malloc(sizeof(char)*10); q = (p + 100) - 99; /* legal? */ free(q - 1); /* legal? */ .... return 0; }
3
1677
by: Kurt | last post by:
i just can't figure out why something im doing is not working correctly.... public interface IInterface { int someProperty { get; set; }
12
1506
by: Mike | last post by:
I want to provide an abstract class to derive all nodes from. In the example below Parent in the base class is of Type Node, but in the custom class I want Parent to return a Type CustomNode. When CustomNode is accessed by a helper class as Node Parent is always null. Is this a poor design, Im sure it's a common problem and I am just missing something. Note: I use virtual for Parent because abstract will not compile. public abstract...
1
281
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum field UserType) and for each type of user a inherited class like SessionMasterUser and SessionNormalUser.
14
1901
by: Daniel | last post by:
Hi guys who just answered me.....it really would have helped if i had written it right. Ok i will use better names to explain my problem. I have this: InterFaceClass ^ ClassA
20
4031
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
32
2374
by: alex.j.k2 | last post by:
Hello all, I have "PRECISION" defined in the preprocessor code and it could be int, float or double, but I do not know in the code what it is. Now if I want to assign zero to a "PRECISION" variable, which of the following lines are correct:
0
8459
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
8378
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
8653
tracyyun
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...
0
7398
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...
1
6206
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
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
4202
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...
1
2786
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
1783
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.