.strip('\n')
时间: 2024-01-15 19:04:40 浏览: 20
`.strip('\n')` 是一个字符串方法,用于移除字符串开头和结尾的指定字符(在这里是换行符 `\n`)。
例如,如果有一个字符串`"apple\n"`,使用`.strip('\n')`方法将移除换行符,返回`"apple"`。
在你的代码中,如果你想移除每个行结尾的换行符,你可以在拆分后的每个行上应用`.strip('\n')`方法。例如:
```python
for words in data:
word = words.split("\n")
word = [w.strip('\n') for w in word]
```
在上面的示例中,我们使用列表推导式遍历拆分后的每个行,并对每个行应用`.strip('\n')`方法。这样,`word`列表中的每个元素将不再包含换行符。
请注意,`.strip()`方法还可以用于移除其他字符,不仅限于换行符。例如,使用`.strip('!')`将移除字符串开头和结尾的感叹号字符。
相关问题
python line.strip
Python中的line.strip()是用来去除字符串中的首尾空格(或其他指定字符)的函数。它可以应用于字符串变量和字符串对象。
例如,如果有一个字符串变量line包含了一个带有首尾空格的字符串,可以使用line.strip()去除这些空格:
```python
line = " Hello, World! "
stripped_line = line.strip()
print(stripped_line) # 输出: "Hello, World!"
```
在这个例子中,strip()函数去除了line字符串变量中的首尾空格,并将结果存储在stripped_line变量中。
此外,strip()函数还可以接受一个可选的参数,用于指定要去除的字符集合。例如,如果想去除字符串中的换行符和制表符,可以使用line.strip('\n\t')。
希望这个解答对你有帮助!如果还有其他问题,请随时提问。
def get_detail(self,page_source): result = [] htmls = etree.HTML(page_source) count = htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div') temp="" list_1 = [] # creat_time ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div[1]/div/div[1]/div[2]/div[2]/a[1]/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b") creat_time ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div/*/div/div[1]/div[2]/div[2]/a[1]/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b") user_name ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div[1]/div/div[1]/div[2]/div[1]/div[2]/a/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b") title ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div[1]/div/div[1]/div[2]/p[1]/a[1]/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b") # text ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div[1]/div/div[1]/div[2]/p[2]/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b") text ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div/div/div[1]/div[2]/p/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b") list_1 =[str(creat_time),str(user_name),str(title),str(text)] result.append(list_1) titles=full3+'\\'+user_name+'.txt' f = open(titles,'a',encoding='utf-8',newline='') f.write(str(creat_time)+"\n") f.write(str(user_name)+"\n") f.write(str(title)+"\n") 怎么修改这段代码能够使得文本内容在不同用户名下存储
您可以将 `titles=full3+'\\'+user_name+'.txt'` 这一行代码放在 `user_name` 的赋值语句下方,这样每次处理一条微博时,都会根据 `user_name` 的不同来创建不同的文件。
修改后的代码如下所示:
```
def get_detail(self, page_source):
result = []
htmls = etree.HTML(page_source)
count = htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div')
temp = ""
list_1 = []
# creat_time ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div[1]/div/div[1]/div[2]/div[2]/a[1]/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b")
creat_time ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div/*/div/div[1]/div[2]/div[2]/a[1]/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b")
user_name ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div[1]/div/div[1]/div[2]/div[1]/div[2]/a/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b")
title ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div[1]/div/div[1]/div[2]/p[1]/a[1]/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b")
# text ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div[1]/div/div[1]/div[2]/p[2]/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b")
text ="".join(htmls.xpath('//*[@id="pl_feedlist_index"]/div[2]/div/div/div[1]/div[2]/p/text()')).strip(" ").strip("\n").strip("\u3000").strip("\u200b")
list_1 =[str(creat_time),str(user_name),str(title),str(text)]
titles = full3+'\\'+user_name+'.txt'
f = open(titles,'a',encoding='utf-8',newline='')
f.write(str(creat_time)+"\n")
f.write(str(user_name)+"\n")
f.write(str(title)+"\n")
f.close()
result.append(list_1)
```
这样就可以实现将不同用户的微博内容存储在不同的文件中了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)