I am trying to write a file but i am getting the following error: TypeError: a bytes-like object is required, not '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)
Can anyone tell me the reason for the error? How can we know the correct syntax of any command and documentation available? I am new to python
When you open a file in python, you must specify its "file mode" - read-only, write-only, read AND write, and if the file is binary. So, in this line:
open("Basketball.csv","r+b")
You opened your file as READ-ONLY, and set the file to be read as BINARY. You should have opened the file as:
open("Basketball.csv","w")
As write and as STRING
Nevertheless, you are manually writting a CSV file - you do not have to do that in Pyhton! Look at this example:
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")
Use python data structures as often as you can!
如果要写字节,则必须如下所示
file.write(bytes(header, encoding="UTF-8"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.