如何将我格式化的数据输出到csv文件?我相信在清理它们时可以写出每一行,但我不确定如何。我试图实现的基本目标是遍历csv的所有行,并仅更改符合for循环条件的行中的项目。然后将所有已更改和未更改的行输出回另一个csv。
import csv
import sys
import re
fileToClean = open(sys.argv[1], 'rb')
readerObj = csv.reader(fileToClean)
for row in readerObj:
for item in row:
if " TB" in item:
newitem = item.replace(" TB","")
result = re.sub('[^0-9]','',newitem)
result = float(newitem) * 1024
result = round(result, 2)
elif " MB" in item:
newitem = item.replace(" MB", "")
result = re.sub('[^0-9]','',newitem)
result = float(result) / 1000
result = round(result, 2)
elif " GB" in item:
newitem = item.replace(" GB", "")
result = re.sub('[^0-9]','',newitem)
result = float(result)
result = round(result, 2)
答案 0 :(得分:2)
完成您要执行的操作的一种简单方法是将文件作为list读取,进行更改,然后将列表写入新文件:
import csv
import sys
import re
fileToClean = open(sys.argv[1], 'rb')
readerObj = list(csv.reader(fileToClean))
# ...iterate through readerObj, changing whatever items you want...
with open("newFile", "w") as csvfile:
wr = csv.writer(csvfile,delimiter=',')
for line in readerObj:
wr.writerow(line)
答案 1 :(得分:1)
#main_menu
{
background-color:#1C1C1C;
float:left;
padding:0px;
width:700px;
height:50px;
line-height:50px;
margin-left:140px;
border-radius:5px;
}
#main_menu .main_list
{
color:white;
list-style-type:none;
float:left;
border-left:1px solid #666;
padding-left:27px;
padding-right:27px;
}
#main_menu .main_list:hover
{
color:#2E9AFE;
}
.main_list ul
{
background-color:white;
width:600px;
position:absolute;
left:150px;
width:700px;
padding:0px;
float:left;
padding-bottom:10px;
}
.main_list ul p
{
color:white;
background-color:#2E9AFE;
margin:0px;
text-align:left;
padding-left:10px;
font-size:20px;
font-weight:bold;
}
.main_list ul ol
{
float:left;
padding:0px;
list-style-type:none;
margin-left:30px;
}
.main_list ul ol li
{
line-height:25px;
font-weight:bold;
font-size:16px;
color:#2E9AFE;
}