python中Object of type bytes is not JSON serializable

源码:爬虫文件中,管道文件中

item['positionName'] = node.xpath("./td[1]/a/text()").extract()[0].encode("utf-8")
self.f.write(content)

此时运行报错

TypeError: Object of type bytes is not JSON serializable

类型字节的对象不是JSON可序列化的,网上查找资料,只需在管道文件中更改文件类型

item['positionName'] = node.xpath("./td[1]/a/text()").extract()[0]
self.f.write(content.encode("utf-8"))
Sure, I can help you with that. Here's the code to create the Student class: ```java import java.util.ArrayList; public class Student { private String name; private ArrayList<String> hobbies; public Student(String name, ArrayList<String> hobbies) { this.name = name; this.hobbies = hobbies; } public String getName() { return name; } public ArrayList<String> getHobbies() { return hobbies; } } ``` And here's the code for the `readFile()` method, which reads from the `student_hobbies.txt` file and returns a list of `Student` objects: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Student> students = readFile("student_hobbies.txt"); // Display the list of students for (Student s : students) { System.out.println(s.getName() + ": " + s.getHobbies()); } } public static ArrayList<Student> readFile(String filename) { ArrayList<Student> students = new ArrayList<>(); try (BufferedReader br = new BufferedReader(new FileReader(filename))) { String line; while ((line = br.readLine()) != null) { String[] parts = line.split(":"); String name = parts[0]; String[] hobbies = parts[1].split(","); ArrayList<String> hobbyList = new ArrayList<>(); for (String h : hobbies) { hobbyList.add(h.trim()); } Student s = new Student(name, hobbyList); students.add(s); } } catch (IOException e) { System.err.println("Error reading file: " + e.getMessage()); } return students; } } ``` This code assumes that the `student_hobbies.txt` file has lines in the format `name:hobby1, hobby2, ...`. It reads each line, splits it into the name and list of hobbies, creates a new `Student` object with those values, and adds it to the list of students. Finally, it displays the list of students and their hobbies.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值