python csv按行而不是列列出 [英] python csv list by rows instead of columns

查看:355
本文介绍了python csv按行而不是列列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本可以接受最终的重定向网址并将其保存到CSV文件中。



脚本在1列中写入代码,例如A1,A3



如何让它按行编写代码A1 B1 C1 D1



请参阅这个红色,我想要的,蓝色是最终的结果,我不想要的(1列中的列表,A1 A3 A5,每个单元格之间有空格)



这是我的最终脚本

  import urllib2 
import csv
import sys

url ='http:// www.test.com'

u = urllib2.urlopen(url)
localfile = open('C:\\test\\file.csv','a')
writer = csv.writer(localfile)
writer.writerow([u.geturl()])
localfile.close()
pre>

解决方案

为什么不自行创建CSV,如果只有一行?

  import urllib2 

url ='http://www.google.com'

u = urllib2.urlopen (url)
localFile = open('C:\\file.csv','ab')
localFile.write(u.geturl()+,)

localFile.close()


I have a script that can take the final redirection url and save it into CSV file.

The script write codes in 1 column for example A1 then A3 then A5

How to make it write the codes by rows for example A1 B1 C1 D1

please see this the red color that what i want, the blue color that is the final result and i don't want it to be like that ( the list in 1 column and goes down A1 A3 A5 and there are a spaces between every cell !! )

this is my final script

import urllib2
import csv
import sys

url = 'http://www.test.com'

u = urllib2.urlopen(url)
localfile = open('C:\\test\\file.csv', 'a')
writer = csv.writer(localfile)
writer.writerow([u.geturl()])
localfile.close()

解决方案

Why not just create CSV by yourself if it will have only one row?

import urllib2

url = 'http://www.google.com'

u = urllib2.urlopen(url)
localFile = open('C:\\file.csv', 'ab')
localFile.write(u.geturl() + ",")

localFile.close()

这篇关于python csv按行而不是列列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆