Python新手 [英] New to Python

查看:87
本文介绍了Python新手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我的无知。我无法得到这个简单的程序来计算

公式。


错误消息是不支持的操作数类型str。我知道我从文件中读取的数据

是一个字符串,但我无法将其转换为

a float或int。


下面是数据文件样本的代码。


#计算SIL文件的毛利


import os.path


in_file = open(" s3cga1.new"," r")


而1:

data = in_file.readline()

如果不是数据:

break


如果数据[0:1] == (&::


nCost = data [99:106]

nPack = data [44:48]


nRetail = data [79:87]

nQuan = data [87:90]

nAllowance = data [147:154]


nGp =((nRetail / nQuan) - ((nCost - nAllowance)/ nPack))/(

nRetail / nQuan)


打印nCost,nPack,nQuan,nRetail,nAllowance

数据文件示例

(00007756725434,''* BREYER STRWBRY ICE'',0007,0006,''56 OZ' ,'000216

'',0004.95,001,2004260,0024.43000,0000000,00000.00,000,0000000,0000000,0004.00000,2004262,2004282),

(00007756725433 ,''* BREYER PEACH ICE CR'',0007,0006,''56 OZ'','000224

'',0004.95,001,2004260,0024.43000,0000000,00000.00,000, 0000000,0000000,0004.00000,2004262,2004282),

解决方案

w写道:

请原谅我的无知。**我不能* * * * * * * * * * * * * * * *



错误消息不支持操作数类型str。** I *知道*我正在从文件中读取的*数据是一个字符串,但我无法将其转换为
一个浮点数或整数。




x = int(y)



x = float(y)

???


Thomas


我试过x = float(nCost)但是一直出错了


ValueError:float的文字无效( ):E ATE TA

"ThomasKrüger" <第************ @ gmx.net>在消息中写道

新闻:ci ************* @ news.t-online.com ...

w写道:

请原谅我的无知。我无法通过这个简单的程序来计算公式。

错误消息是不支持的操作数类型str。我知道我从文件中读取的数据是一个字符串,但我无法将其转换为浮点数或整数。



x = int (y)

x =浮动(y)
???托马斯




在< YU *************** @ fe61.usenetserver.com> ;, Jimmie Webb写道:

我试过x = float(nCost)但是一直出错。

ValueError:float()的文字无效:EATE TA




也许是切片值是错的?从字符串中挑选出来后,你打印了所有的

变量,以确保它们包含

你期望的吗?


Ciao ,

Marc''BlackJack''Rintsch


excuse my ignorance. I cannot get this simple program to calculate
the formula.

The error message is unsupported operand type(s) str. I know the data
that I''m reading from the file is a string but I cannot convert it to
a float or int.

The code a sample form the data file are below.

# Calculate Gross Profit from SIL File

import os.path

in_file = open( "s3cga1.new", "r")

while 1:
data = in_file.readline()
if not data:
break

if data[0:1] == "(":

nCost = data[ 99:106]
nPack = data[ 44:48]

nRetail = data[ 79:87]
nQuan = data[ 87:90]
nAllowance = data[ 147:154]

nGp = ( ( nRetail / nQuan ) - ( ( nCost - nAllowance) / nPack ) ) / (
nRetail / nQuan )

print nCost, nPack, nQuan, nRetail, nAllowance
EXAMPLE OF DATA FILE
(00007756725434,''*BREYER STRWBRY ICE '',0007,0006,'' 56 OZ '',''000216
'',0004.95,001,2004260,0024.43000,0000000,00000.00, 000,0000000,0000000,0004.00000,2004262,2004282),
(00007756725433,''*BREYER PEACH ICE CR'',0007,0006,'' 56 OZ '',''000224
'',0004.95,001,2004260,0024.43000,0000000,00000.00, 000,0000000,0000000,0004.00000,2004262,2004282),

解决方案

w wrote:

excuse my ignorance.**I*cannot*get*this*simple*program*to*ca lculate
the formula.

The error message is unsupported operand type(s) str.**I*know*the*data
that I''m reading from the file is a string but I cannot convert it to
a float or int.



x = int(y)
or
x = float(y)
???

Thomas


I tried x = float(nCost) but keep getting an error

ValueError: Invalid literal for float(): EATE TA
"Thomas Krüger" <th************@gmx.net> wrote in message
news:ci*************@news.t-online.com...

w wrote:

excuse my ignorance. I cannot get this simple program to calculate
the formula.

The error message is unsupported operand type(s) str. I know the data
that I''m reading from the file is a string but I cannot convert it to
a float or int.



x = int(y)
or
x = float(y)
???

Thomas




In <YU***************@fe61.usenetserver.com>, Jimmie Webb wrote:

I tried x = float(nCost) but keep getting an error

ValueError: Invalid literal for float(): EATE TA



Maybe the slice values are just wrong? Have you printed all your
variables after picking them from the string to make sure they contain
what you expect?

Ciao,
Marc ''BlackJack'' Rintsch


这篇关于Python新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆