写入文件时出错

时间:2017-09-01 17:00:52

标签: python python-3.x

我正在尝试编写一个文件,但是我收到以下错误: TypeError:需要类似字节的对象,而不是' str'

import requests, pandas
from bs4 import BeautifulSoup

r = requests.get("https://www.basketball-reference.com/players/a/")
c = r.content
soup = BeautifulSoup(c, "html.parser")
full_record_heading = soup.findAll("tr")
full_record = soup.findAll("tr")
playerdata = ""
playerdata_saved = ""
for record in full_record:
    playerdata = ""
    for player in record.findAll("td"):
        playerdata = playerdata +","+player.text
    playerdata_saved = playerdata_saved + playerdata[1:]+("\n")
# print(playerdata_saved)

header="From,To,Pos,Ht,Wt,Birth Date,College"
file=open("Basketball.csv","r+b")
file.write(header)

有人能告诉我错误的原因吗?我们怎样才能知道任何命令和文档的正确语法?我是python的新手

2 个答案:

答案 0 :(得分:0)

在python中打开文件时,必须指定其文件模式" - 只读,只写,读和写,以及文件是否为二进制文件。所以,在这一行:

open("Basketball.csv","r+b")

您以READ-ONLY方式打开文件,并将文件设置为BINARY。 您应该已将文件打开为:

open("Basketball.csv","w")

写作和STRING

然而,您手动编写CSV文件 - 您不必在Pyhton中执行此操作!看看这个例子:

import requests
import pandas  # Always import in different lines
from bs4 import BeautifulSoup

r = requests.get("https://www.basketball-reference.com/players/a/")
c = r.content
soup = BeautifulSoup(c, "html.parser")
full_record_heading = soup.findAll("tr")
full_record = soup.findAll("tr")

# Initialize your data buffer
my_data = []

# For each observation in your data source
for record in full_record:
    # We extract a row of data
    observation = record.findAll("td")
    # Format the row as a dictionary - a "python hashmap"
    dict_observation = {
        "From": observation[0],
        "To": observation[1],
        "Pos": observation[2],
        "Ht": observation[3],
        "Wt": observation[4],
        "Birth Date": observation[5],
        "College": observation[6]
    }
    # Add the row to our DataFrame buffer
    my_data.append(dict_observation)
# Now our DataFrame buffer contains all our data.
# We can format it as a Pandas DataFrame
dataframe = pandas.DataFrame().from_dict(my_data)

# Pandas DataFrames can be turned into CSVs seamlessly. Like:
dataframe.to_csv("Basketball.csv", index=False)

# Or even MS Excel:
dataframe.to_excel("Basketball.xlsx")

尽可能多地使用python数据结构!

答案 1 :(得分:0)

如果你想写字节,你必须像下面那样

file.write(bytes(header, encoding="UTF-8"))