简体   繁体   English

ValueError:未知的URL类型:'h'

[英]ValueError: unknown url type: 'h'

Ok, so I'm trying my best to run this program, and I BELIEVE this function is the problem 好的,所以我正在尽力运行此程序,并且我相信此功能是问题所在

urls = ('http://3.bp.blogspot.com/-SC-w7eTgpM0/URE9NsI_nuI/AAAAAAAAAGE/YmlWnimNuPM/s1600/7957178556_001939ffc5_z.jpg')
for addr in urls:
        get_img_from_web(addr)
images, names = scan_dir()
lum_values = []
for i in range(256):
        lum_values.append(i)
header = 'Jack Tompkins\n'+','.join(str(lum) for lum in lum_values)+'\n\n'
with open('p1TompkinsHistogram.csv', 'w') as f:
        f.write(header)
        for i, im in enumerate(images):
           data = get_data(im)
           path = names[i]
           last_slash = path.rfind('/')
           name = path[last_slash+1:]
           f.write(name +',' + im.mode + ',' + data + ',')
           h_r, h_g, h_b = get_histograms(im)
           f.write(get_histogram_data(h_r, h_g, h_b) + '\n')
           f.write(','.join(str(i) for i in h_r) + '\n')
           f.write(','.join(str(i) for i in h_g) + '\n')
           f.write(','.join(str(i) for i in h_b) + '\n')

` `

This is the end of the error I get 这是我得到的错误的结尾

raise ValueError("unknown url type: %r" % self.full_url)
ValueError:unknown url type: 'h'

Any thoughts? 有什么想法吗?

Your problem is here: 您的问题在这里:

urls = ('http://3.bp.blogspot.com/-SC-w7eTgpM0/URE9NsI_nuI/AAAAAAAAAGE/YmlWnimNuPM/s1600/7957178556_001939ffc5_z.jpg')
for addr in urls:

You meant to define urls as a tuple, but it is in fact a single string. 您打算将urls定义为元组,但实际上它是单个字符串。 Because the parentheses don't include any commas, Python sees it as a single expression to be evaluated, much as (2 + 2) is 4 and not a tuple containing 4. Strings are iterable and yield their constituent characters, so addr is each character from the string. 由于括号不包含任何逗号,因此Python将其视为要评估的单个表达式,因为(2 + 2)为4,而不是包含4的元组。字符串是可迭代的并产生其组成字符,因此addr是每个字符串中的字符。 So you try to open h , then t , then t , and so on. 因此,您尝试打开h ,然后打开t ,然后打开t ,依此类推。 Or you would, if trying to open h didn't give you an error. 否则,如果尝试打开h并没有给您错误,则可能会。

The solution is to write urls using a trailing comma, ensuring that Python sees it as a tuple, or to just use square brackets and make it a list. 解决方案是使用逗号结尾的urls ,以确保Python将其视为元组,或者仅使用方括号并将其制成列表。 Or of course, use more than one item. 或者,当然,可以使用多个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM