在本文中,您将会了解到关于Python:AttributeError:'_io.TextIOWrapper'对象没有属性'split'的新资讯,同时我们还将为您解释python对象没有这个属性的相关在
在本文中,您将会了解到关于Python:AttributeError:'_io.TextIOWrapper'对象没有属性'split'的新资讯,同时我们还将为您解释python对象没有这个属性的相关在本文中,我们将带你探索Python:AttributeError:'_io.TextIOWrapper'对象没有属性'split'的奥秘,分析python对象没有这个属性的特点,并给出一些关于059 Python:Tensorflow - AttributeError:'OwnedIterator'对象没有属性'string_handle'、AttributeError: '_io.TextIOWrapper' 对象没有属性 'fp'、AttributeError:'list'对象没有属性'split'无法找出错误、AttributeError:'NoneType'对象没有属性'text',(xml,python)的实用技巧。
本文目录一览:- Python:AttributeError:'_io.TextIOWrapper'对象没有属性'split'(python对象没有这个属性)
- 059 Python:Tensorflow - AttributeError:'OwnedIterator'对象没有属性'string_handle'
- AttributeError: '_io.TextIOWrapper' 对象没有属性 'fp'
- AttributeError:'list'对象没有属性'split'无法找出错误
- AttributeError:'NoneType'对象没有属性'text',(xml,python)
Python:AttributeError:'_io.TextIOWrapper'对象没有属性'split'(python对象没有这个属性)
我有一个文本文件,我们称之为它goodlines.txt
,我想加载它并创建一个包含文本文件中每一行的列表。
我尝试使用这样的split()
过程:
>>> f = open(''goodlines.txt'')>>> mylist = f.splitlines()Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: ''_io.TextIOWrapper'' object has no attribute ''splitlines''>>> mylist = f.split()Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: ''_io.TextIOWrapper'' object has no attribute ''split''
为什么会出现这些错误?那不是我的用法split()
吗?(我正在使用python 3.3.2
)
答案1
小编典典您正在str
打开文件对象上使用方法。
您可以通过简单地调用list()
文件对象来将文件读取为行列表:
with open(''goodlines.txt'') as f: mylist = list(f)
这 确实 包括换行符。您可以剥离列表理解中的那些:
with open(''goodlines.txt'') as f: mylist = [line.rstrip(''\n'') for line in f]
059 Python:Tensorflow - AttributeError:'OwnedIterator'对象没有属性'string_handle'
如何解决059 Python:Tensorflow - AttributeError:''OwnedIterator''对象没有属性''string_handle''?
代码如下:
iterator = tf.compat.v1.data.make_one_shot_iterator(batched_dataset)
handle = iterator.string_handle()
我尝试运行 this github project 的 Captcha 破解代码,尽管我不得不修改代码的许多部分以解决兼容性问题,但我还是遇到了这个错误,错误说:
AttributeError: ''OwnedIterator'' object has no attribute ''string_handle''
我已经尝试了超过 1 天,但找不到任何解决此问题的方法希望有人帮助我运行代码,没有错误!
我使用的是 Pycharm 和 python 以及 Tensorflow 版本 = 2.4.1
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
AttributeError: '_io.TextIOWrapper' 对象没有属性 'fp'
如何解决AttributeError: ''_io.TextIOWrapper'' 对象没有属性 ''fp''?
当我尝试将 txt 文件发送到 discord 网络钩子时出现此错误。 (python3)
import os
import subprocess
import requests
import discord
import dhooks
from dhooks import Webhook,Embed
hook = Webhook("https://discord.com/api/webhooks/816226588005367849/C_qZ-zGf80vucvO6RBQbL9tQaECdhaCwSp8im5ZkCotwEPzdwqgkYm1jmrfhlcafBhPI")
discord_txt = open("data.txt","r+")
hook.send(file=discord_txt)
解决方法
根据 the docs,您应该使用 File
中的 dhooks
类:
from dhooks import Webhook,File
hook = Webhook("https://discord.com/api/webhooks/...")
Discord_txt = File("data.txt")
hook.send(file=Discord_txt)
AttributeError:'list'对象没有属性'split'无法找出错误
如何解决AttributeError:''list''对象没有属性''split''无法找出错误?
问题是要继续问一个人说出自己喜欢的网站,并在他们输入完成后停止。我们要提取“ www”。和“ .com”,并将该部分附加到列表中,最后打印该列表。我不知道我在做什么错。
int
解决方法
使用正则表达式可以轻松完成此操作。但是,看来您是python新手。因此,以下更正的代码可能会帮助您。
import json
username = "jose"
email = "some_email"
password = "1234"
url = "some_url"
payload = json.dumps({"username": username,"email":email,"password":password},indent=4)
headers = { ''Content-Type'': ''application/json''}
response = requests.request("POST",url,headers=headers,data=payload)
print(response.text.encode(''utf8''))
,
根据您的问题,我会执行此功能
myweb=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
print(myweb)
break
else:
myweb.append(mywebsite)
continue
# allocating enough space
mywebsite = [0]*len(myweb)
# extract it
for i in range(len(myweb)):
# split based on www.domain.com
mywebsite[i] = myweb[i].split(''.'')[1]
print(mywebsite)
,
在列表中的每个项目上使用正则表达式
>>> import re
>>> website = ''www.amazon.com''
>>> website = re.search(''www\.(.*)\.com'',website).group(1)
>>> website
''amazon''
,
如果所有输入的模式都是www.SOME_WEB_SITES.com,那么您可以使用
w2
还要考虑子域的情况,例如:
www.a.com->一个
www.a.b.com-> a.b
N = 3
w2 = Window.partitionBy(''product'',''specialty'').orderBy(''date'').rowsBetween(-N+1,0)
df_new = df2.select("*",F.round(F.sum(''sales'').over(w2)/N,2).alias(f''{N}month_avg_sales''))
+-------+---------+----------+-----+----------------+
|product|specialty| date|sales|3month_avg_sales|
+-------+---------+----------+-----+----------------+
| A| ENT|2019-08-01| 50| 16.67|
| A| ENT|2019-09-01| 65| 38.33|
| A| ENT|2019-10-01| 0| 38.33|
| A| ENT|2019-11-01| 40| 35.0|
| A| pharma|2019-03-01| 50| 16.67|
| A| pharma|2019-04-01| 60| 36.67|
| A| pharma|2019-05-01| 70| 60.0|
| A| pharma|2019-06-01| 0| 43.33|
| A| pharma|2019-07-01| 0| 23.33|
| A| pharma|2019-08-01| 80| 26.67|
+-------+---------+----------+-----+----------------+
,
您使用python字符串.strip()
函数执行此操作。
myweb=[]
while True:
mywebsite=input("what is your favorite website?")
if mywebsite=="done":
for x in myweb:
print(x.strip("www." + ".com")) #Strips www. and .com from the string.
break
else:
myweb.append(mywebsite)
AttributeError:'NoneType'对象没有属性'text',(xml,python)
如何解决AttributeError:''NoneType''对象没有属性''text'',(xml,python)?
我有一个xml文件,
<?xml version="1.0" encoding="UTF-8"?>
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
<edit-config category = "xyz">
<target category="cooking">
<author>Giada De Laurentiis</author>
</target>
</edit-config>
</rpc>
我要打印作者姓名,我是这样写的,
from xml.etree.ElementTree import ElementTree
tree = ElementTree()
root = tree.parse("test.xml")
print (''Name: '',root.find(''rpc/edit-conf/target/author'').text)
我遇到以下错误,我缺少什么吗?
解决方法
见下文
import xml.etree.ElementTree as ET
xml = ''''''<?xml version="1.0" encoding="UTF-8"?>
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
<edit-config category = "xyz">
<target category="cooking">
<author>Giada De Laurentiis</author>
</target>
</edit-config>
</rpc>''''''
root = ET.fromstring(xml)
author = root.find(''.//{urn:ietf:params:xml:ns:netconf:base:1.0}author'')
print(author.text)
输出
Giada De Laurentiis
,
可以通过以下XPATH访问该标签:
a = root.find(''./{urn:ietf:params:xml:ns:netconf:base:1.0}edit-config/{urn:ietf:params:xml:ns:netconf:base:1.0}target/{urn:ietf:params:xml:ns:netconf:base:1.0}author'')
# or ".//{urn:ietf:params:xml:ns:netconf:base:1.0}author" for skip middle sub elements and find subelements,on all levels.
Out[7]: <Element ''{urn:ietf:params:xml:ns:netconf:base:1.0}author'' at 0x7f01f9c1a220>
print(a.text)
rpc标记中的 xmlns
引用XML namespace(用于防止在尝试混合XML时发生冲突),并且在rool元素的XML文件中,您将urn:ietf:params:xml:ns:netconf:base:1.0
视为命名空间。 / p>
要逐步找到正确的Xpath,请尝试:
In [1]: root.find(''./'')
Out[1]: <Element ''{urn:ietf:params:xml:ns:netconf:base:1.0}edit-config'' at 0x7f01f9c16ef0>
In [2]: root.findall(''./{urn:ietf:params:xml:ns:netconf:base:1.0}edit-config/'')
Out[2]: [<Element ''{urn:ietf:params:xml:ns:netconf:base:1.0}target'' at 0x7f01fa2a4ea0>]
In [3]: root.findall(''./{urn:ietf:params:xml:ns:netconf:base:1.0}edit-config/{urn:ietf:params:xml:ns:netconf:base:1.0}target/'')
Out[3]: [<Element ''{urn:ietf:params:xml:ns:netconf:base:1.0}author'' at 0x7f01f9c1a220>]
In [4]: root.findall(''./{urn:ietf:params:xml:ns:netconf:base:1.0}edit-config/{urn:ietf:params:xml:ns:netconf:base:1.0}target/{urn:ietf:params:xml:ns:netconf:base:1.0}author'')
Out[4]: [<Element ''{urn:ietf:params:xml:ns:netconf:base:1.0}author'' at 0x7f01f9c1a220>]
,
xml标记为<edit-config category = "xyz">
代码正在搜索''/edit-conf/''
使它们相同就可以了
今天关于Python:AttributeError:'_io.TextIOWrapper'对象没有属性'split'和python对象没有这个属性的讲解已经结束,谢谢您的阅读,如果想了解更多关于059 Python:Tensorflow - AttributeError:'OwnedIterator'对象没有属性'string_handle'、AttributeError: '_io.TextIOWrapper' 对象没有属性 'fp'、AttributeError:'list'对象没有属性'split'无法找出错误、AttributeError:'NoneType'对象没有属性'text',(xml,python)的相关知识,请在本站搜索。
本文标签: