简体   繁体   English

Python:逐行读取文本变量(不是文件)

[英]Python: read text variable line by line (not file)

I do have: 我有:

url="https://mywebpage.com/content"
text=requests.get(url,stream=True).text

for line in text:
  print "Line "+line

I got each letter in a different line (instead of line). 我在不同的行(而不是行)中得到了每个字母。 I also can not use iterator req.iter_lines because I need to process the results multiple times looking for different data. 我也不能使用迭代器req.iter_lines,因为我需要多次处理结果以查找不同的数据。

Any hints? 有什么提示吗?

You should use split and strip , in case you have windows style line breaks ( \\r ) 如果您有Windows样式的换行符( \\r ),则应使用splitstrip

for line in text.split('\n'):
  line = line.strip('\r')
  print ("Line "+line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM