【问题标题】:Python. How to fix error: "print tablecsv.read() AttributeError: 'tuple' object has no attribute 'read"Python。如何修复错误:“print tablecsv.read() AttributeError: 'tuple' object has no attribute 'read”
【发布时间】:2011-09-14 08:05:04
【问题描述】:

我学习 Python 并尝试为 html 表做 parcer,然后我不会创建 .csv 文件以将数据导入 mySQL。

>>> htmlread = handlestatbydate.read()
>>> soup = BeautifulSoup("".join(htmlread))
>>> souptable = soup('tbody', limit=2)[1].findAll('tr')
>>> souptablestr = ''.join(str(t) for t in souptable)
>>> reclearbyonce = re.compile('</tr><tr>\n|^<tr>\n|</tr>$')
>>> recleartd = re.compile(r'</td>|<td.*?>')
>>> retdtd = re.compile('""| ')
>>> soupclearbyonce = reclearbyonce.sub('', souptablestr)
>>> soupcleartd = recleartd.sub('"', soupclearbyonce)
>>> souptdtd = retdtd.sub('","', soupcleartd)
>>> print souptdtd
"59","00059413","00059413","70000000001","2011-08-22","18:01:48","0:07","0.45"
"60","00059413","00059413","70000000002","2011-08-22","18:49:48","0:43","1.95"
"61","00059413","00059413","70000000003","2011-08-22","18:52:50","5:07","11.70"
"62","00059413","00059413","70000000003","2011-08-22","19:02:47","4:10","9.75"

然后,我创建 csv 文件并出现错误。

>>> tablecsv = file(r'/tmp/table.csv', 'w')
>>> tablecsv.write("".join(souptdtd))
>>> tablecsv = (r'/tmp/table.csv', 'r')
>>> print tablecsv.read()

    print tablecsv.read()
AttributeError: 'tuple' object has no attribute 'read

不幸的是,我无法理解何时以及如何创建元组。有人可以解释我什么时候出错以及如何解决吗?

【问题讨论】:

  • 天啊...舒尔。我该睡觉了,愚蠢的错误 =) 非常感谢!

标签: python csv


【解决方案1】:

您在这里错过了方法名称:tablecsv = (r'/tmp/table.csv', 'r'),即您可能想要打开文件。例如tablecsv = open(r'/tmp/table.csv', 'r')

您还应该在写入后关闭文件,然后再使用tablecsv.close() 读取文件

如果您只有括号中的项目列表,例如(r'/tmp/table.csv', 'r') 然后这会创建一个元组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-18
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 2021-04-09
    • 2021-11-08
    • 1970-01-01
    相关资源
    最近更新 更多