url = FINAM_URL + symbol+"_" + start_date_rev + "_" + end_date_rev + ".csv?" + params
txt=str(urlopen(url).readlines())
local_file = open('company_quotes.txt', "w")
local_file.write(txt)
local_file.close()
我希望我的文件中没有b',\ r,\ n'符号,并且看起来像这样(多行):
答案 0 :(得分:1)
b
表示字节对象,您必须使用.decode( "utf-8" )
将字节转换为字符串。您还必须使用.strip()
才能删除字符串末尾的多余字符,例如\n\r
。
因此使用:
for line in lines:
clean_line = line.strip().decode( "utf-8" )
答案 1 :(得分:1)
从该URL获取的数据为字节,因此您可以将数据直接以二进制格式写入文件,如下所示:
\r\n
var total = 0
var published = 0
service
.getThings()
.doOnNext { total++ }
.flatMap { shouldPublish(it) } // Returns empty() or just(it)
.doOnNext { published++ }
.subscribe { publishToSqs(it) }
println("Published $publish out of $total")
符号是换行符,因此,如图所示写入它们将导致文件正确写入多行。
答案 2 :(得分:0)
使用replace()
with open('company_quotes.txt', 'r') as f:
for line in f.readlines:
line = line.replace('\r\n', '')
答案 3 :(得分:0)
txt=str(urlopen(url).readlines())
是问题所在。您需要在每一行上调用.decode()
而不是使用str()
。例如:
with open('company_quotes.txt', "w") as local_file:
for line in urlopen(url).readlines():
local_file.write(line.decode())