user_list如何存储到txt中
时间: 2024-04-29 17:27:04 浏览: 11
将user_list转化为字符串,并使用Python中的文件操作函数将其写入txt文件中。例如,可以使用以下代码将user_list写入名为"users.txt"的文件中:
```
user_list = ["User1", "User2", "User3"]
# convert user_list to string
user_str = '\n'.join(user_list)
# write string to txt file
with open('users.txt', 'w') as file:
file.write(user_str)
```
这将在当前目录下生成一个"users.txt"文件,其中包含每个用户的名称,每个名称占一行。
相关问题
import requests from bs4 import BeautifulSoup import random import time main_url="http://www.xsbiquge.org/book/11432/" headers={ 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0' } main_req=requests.get(main_url,headers=headers) title_list=[] chapters_list=[] main_bs4=BeautifulSoup(main_req.text,"html.parser") #print(main_bs4.text) main_find_list=main_bs4.find_all("div",class_="info-chapters flex flex-wrap")[1].select("a") #print(main_find_list[1].text) #print(len(main_find_list)) for i in main_find_list: #print(i) title_list.append(i.text) chapters_list.append("www.xsbiquge.org"+i["href"]) #print(title_list) #print(chapters_list) fp=open("./shu.txt","w",encoding="utf-8") for i,chapter_url in enumerate(chapters_list): chapter_req=requests.get(chapter_url,headers=headers) chapter_bs4=BeautifulSoup(chapter_req.text,"html.parser") print(chapter_bs4.select("#article"))
好的,我看到你的代码了。代码中存在两个问题:
1. 第一行的代码需要在 `import` 关键字前添加 `from` 关键字,正确的写法为:
```python
from requests import requests
from bs4 import BeautifulSoup
import random
import time
```
2. `chapters_list` 中存储的是每个章节的链接地址,但是在输出章节内容时,代码中使用了 `chapter_bs4.select("#article")`,这个选择器是用来选择 id 为 `article` 的元素,但是章节内容并没有这个 id,所以需要修改这一行代码。你可以使用 `chapter_bs4.select("div#content")` 来选择章节内容所在的元素。
修改后的代码如下所示:
```python
from requests import requests
from bs4 import BeautifulSoup
import random
import time
main_url="http://www.xsbiquge.org/book/11432/"
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0'
}
main_req=requests.get(main_url,headers=headers)
title_list=[]
chapters_list=[]
main_bs4=BeautifulSoup(main_req.text,"html.parser")
#print(main_bs4.text)
main_find_list=main_bs4.find_all("div",class_="info-chapters flex flex-wrap")[1].select("a")
#print(main_find_list[1].text)
#print(len(main_find_list))
for i in main_find_list:
#print(i)
title_list.append(i.text)
chapters_list.append("http://www.xsbiquge.org"+i["href"])
#print(title_list)
#print(chapters_list)
fp=open("./shu.txt","w",encoding="utf-8")
for i,chapter_url in enumerate(chapters_list):
chapter_req=requests.get(chapter_url,headers=headers)
chapter_bs4=BeautifulSoup(chapter_req.text,"html.parser")
content = chapter_bs4.select("div#content")[0].text
fp.write(title_list[i] + "\n")
fp.write(content + "\n\n")
time.sleep(random.uniform(1, 3))
print(title_list[i] + "下载完成!")
fp.close()
```
希望这个修改能够解决你的问题,如果还有其他问题,请随时问我。
c语言读取数据文件的函数原型为:int real_file(struct User,List[])。函数功能:从APCOM.txt文件中读取航班信息,并返回航班信息。其中,List用于存放读取到的航班信息。
函数原型应该是这样的:
```c
int read_file(struct User user, List list[]);
```
函数的功能是从文件APCOM.txt中读取航班信息,并将读取到的航班信息保存在List数组中。参数user是一个结构体,用于存储当前用户的信息。参数list是一个数组,用于存储读取到的航班信息。
具体实现的话,可以使用标准库函数fopen打开文件,然后使用fscanf逐行读取文件中的数据,最后将读取到的数据存储在List数组中。读取完成后,使用fclose关闭文件。具体代码如下:
```c
#include <stdio.h>
#define MAX_FLIGHTS 100 // 最大航班数
typedef struct {
// 定义航班信息结构体
char flight_no[10]; // 航班号
char departure[20]; // 起飞地
char destination[20]; // 目的地
char date[10]; // 日期
} Flight;
typedef struct {
// 定义用户信息结构体
char username[20];
char password[20];
} User;
int read_file(User user, Flight list[]) {
FILE *fp;
int count = 0;
fp = fopen("APCOM.txt", "r");
if (fp == NULL) {
printf("Failed to open file.\n");
return -1;
}
// 读取文件中的数据
while (fscanf(fp, "%s%s%s%s", list[count].flight_no, list[count].departure, list[count].destination, list[count].date) != EOF) {
count++;
if (count == MAX_FLIGHTS) {
printf("Too many flights.\n");
break;
}
}
fclose(fp);
return count;
}
int main() {
User user = {"admin", "123456"};
Flight list[MAX_FLIGHTS];
int count;
count = read_file(user, list);
if (count >= 0) {
printf("%d flights read.\n", count);
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)