Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-在字符串上打印变量_Python_String_Variables - Fatal编程技术网

Python-在字符串上打印变量

Python-在字符串上打印变量,python,string,variables,Python,String,Variables,我使用的是Python2.7,我遇到了一个以前从未遇到过的问题,当我打印某个字符串,然后在同一行上打印一个变量时,该变量会打印在字符串上。e、 g.脚本的编码类似于soprint'IP Rating=',ipRating,命令提示符中的输出将是'IP20ating='。我不知道为什么会发生这种情况,但我在同一个脚本中为不同的变量和字符串编写了相同的代码,它们都如预期的那样出现,我尝试过重命名变量并更改字符串,但仍然没有区别,以前有人遇到过这种错误,或者知道为什么会发生这种情况吗?如果需要,我可以

我使用的是Python2.7,我遇到了一个以前从未遇到过的问题,当我打印某个字符串,然后在同一行上打印一个变量时,该变量会打印在字符串上。e、 g.脚本的编码类似于so
print'IP Rating=',ipRating
,命令提示符中的输出将是
'IP20ating='
。我不知道为什么会发生这种情况,但我在同一个脚本中为不同的变量和字符串编写了相同的代码,它们都如预期的那样出现,我尝试过重命名变量并更改字符串,但仍然没有区别,以前有人遇到过这种错误,或者知道为什么会发生这种情况吗?如果需要,我可以发布代码。 非常感谢:)

编辑 下面是代码-我知道我可能已经重复了几次,其中有不必要的库,但我的工作方式是导入所有可能需要的库,然后在最后删除不必要的代码

from bs4 import BeautifulSoup as Soup
from bs4 import BeautifulSoup
from urllib import urlopen
import webbrowser
import httplib
import urllib2
import urllib
import string
import mylib
import xlrd
import glob
import xlwt
import bs4
import sys
import os
import re

print '\nStarting Web Search'
found = False
while found == False:
    excelFile = "F:\\len\\web sheets completed\\csv formatted\\imported\\re-imported\\Import Corrections\\saxby web spreadsheet.xls"
    try:
        inFi = xlrd.open_workbook(excelFile)
        found = True
    except IOError: 
        print 'File not found.' 
inFi = xlrd.open_workbook(excelFile)
inWS = inFi.sheet_by_index(0)
headers = mylib.getHeader(inWS)
supplyHead = mylib.findHeader('Supplier Part Ref', headers)
saxbeginurl = "http://www.saxbylighting.com/index.php?pg=search&ser="
badLink = "index.php?pg=search&ser=10180&next=0"
resLink = "http://www.saxbylighting.com/images/ProductImages/Zoomed/"
overCount = 0
for t in range(524,534):
    projection = 0
    ipRating = 0
    diameter = 0
    width = 0
    weight = 0
    length = 0
    height = 0
    i = 0
    w = 0
    l = 0
    h = 0
    d = 0
    p = 0
    x = 0
    iP = 0
    wei = 0
    imgStock = str(inWS.cell(t, supplyHead).value.encode('latin-1'))
    overCount = overCount + 1
    print '\n',imgStock
    if imgStock == '3TRAWI':
        url = 'http://www.saxbylighting.com/index.php?pg=details&prod=53'
    elif imgStock == '10313':
        url = 'http://www.saxbylighting.com/index.php?pg=details&prod=204'
    else:
        url = saxbeginurl + imgStock
    html_page = urllib2.urlopen(url)
    soup = BeautifulSoup(html_page)
    img_tags = soup.find_all("img")
    the_image_tag = soup.find("img", src='/images/dhl_logo.png')
    try:
        for dataSheet in soup.find('div',{'class':'panes'}):
            #print dataSheet, ' -- ', str(i)
            i = i + 1
            if i == 4:
                reqData = str(dataSheet).split('<img', 1)[0]
                first_Data = reqData.replace('<br/>','\n')
                second_Data = first_Data.replace('<b>','')
                third_Data = second_Data.replace('</b>','')
                fourth_Data = third_Data.replace(':',': ')
                dataList = fourth_Data.split('\n')
                #print dataList
                for information in dataList:
                    if 'Weight' in dataList[wei]:
                        pre_Weight = dataList[wei]
                        sec_weight = str(pre_Weight).replace('Weight :','')
                        weight = sec_weight.replace(' ','')
                    wei += 1
                    if 'IP' in dataList[iP]:
                        ipRating = str(dataList[iP])
                    iP += 1
        for product_Dimensions in dataList:
            if 'Product dimensions :' in dataList[x]:
                #print dataList[x]
                dimensionList = str(dataList[x]).replace('mm','mm:')
                #print dimensionList
                prelim_Dimensions = dimensionList.replace('Product dimensions :','')
                first_Dimensions = prelim_Dimensions.replace('cm','0mm')
                sec_Dimensions = first_Dimensions.replace('  ',' ')
                third_Dimensions = sec_Dimensions.strip()
                dimenList = third_Dimensions.split('mm:')
                #print dimenList
                for project in dimenList:
                    if 'Proj' in dimenList[p]:
                        pre_pro = str(dimenList[p]).replace('Proj','')
                        sec_pro = pre_pro.replace(':','')
                        thro_pro = sec_pro.replace(' ','')
                        projection = thro_pro
                    elif p == len(dimenList):
                        print 'Projection not found'
                    p += 1
                for diamet in dimenList:
                    if 'dia' in dimenList[d]:
                        pre_dia = str(dimenList[d]).replace('dia','')
                        sec_dia = pre_dia.replace(':','')
                        third_dia = sec_dia.replace(' ','')
                        diameter = third_dia
                    elif d == len(dimenList):
                        print 'Diameter not found'
                    d += 1
                for heig in dimenList:
                    if 'H:' in dimenList[h]:
                        pre_hei = str(dimenList[h]).replace('H','')
                        sec_hei = pre_hei.replace(':','')
                        third_hei = sec_hei.replace(' ','')
                        height = third_hei
                    elif h == len(dimenList):
                        print 'Height not found'
                    h += 1
                for lent in dimenList:
                    if 'L:' in dimenList[l]:
                        pre_leng = str(dimenList[l]).replace('L','')
                        sec_leng = pre_leng.replace(':','')
                        third_leng = sec_leng.replace(' ','')
                        length = third_leng
                    elif l == len(dimenList):
                        print 'Length not found'
                    l += 1
                for wid in dimenList:
                    if 'W:' in dimenList[w]:
                        pre_wid = str(dimenList[w]).replace('W','')
                        sec_wid = pre_wid.replace(':','')
                        third_wid = sec_wid.replace(' ','')
                        width = third_wid
                    elif w == len(dimenList):
                        print 'Width not found'
                    w += 1
            x += 1
        print 'IP Rating = ', ipRating
        print 'Weight = ', weight
        print 'Projection = ', projection, 'mm'
        print 'Diameter = ',diameter, 'mm'
        print 'Length = ',length, 'mm'
        print 'Height = ',height, 'mm'
        print 'Width = ',width, 'mm'
    except TypeError:
        print 'Type Error... skipping this product and carrying on.'

这是做你正在做的事情的正确方法

print('IP Rating = %s' % ipRating)

这只是代码末尾所有
print
语句中的一个示例


如果要打印字符串变量,请使用%s或%d。如果您还有任何问题,请提问。

我强烈怀疑您认为是
IP20
的数据
ipRating
实际上是
\rIP20
。也就是说:在变量的开头有一个零散的0x13回车字符。回车符将打印位置移动到行首,然后变量将覆盖之前打印的内容

您可以通过添加以下行来测试这是否是问题:

ipRating = ipRating.replace("\r", "")

在打印语句之前。

yes代码被请求,不要犹豫,请发布代码,您可以尝试一下:打印'IP Rating=',str(ipRating),所以您说ipRating变量的值为20,但打印时您得到'IP20ating='。。对吗?代码是这样的,@Trygve变量已经被格式化为字符串了,重新格式化会有什么不同吗?@Srivatsan就是这样,除了ipRating变量不总是20如果你打印“IP Rating={}”,会发生什么。格式化(ipRating)他使用的是Python2.7这是在Python2.7上完成的。它还支持基于功能的打印。如果您打算建议更改打印功能,那么更改格式功能是否也有意义<代码>打印('IP等级={0}).format(ipRating))@Remolten相同的输出,感谢您的建议和帮助though@CasualDemon同样的输出,该死的,这太令人沮丧了。hahaI现在就试试这个,让你知道输出是好的。但是,你应该试着找出迷路的角色是从哪里来的,而不是简单地掩盖裂缝。
print('IP Rating = %d' % ipRating)
ipRating = ipRating.replace("\r", "")