我有一个csv文件,如下所示:
Names, Size, State, time1, time2,
S1, 22, MD , 0.022, , 523.324
S2, 22, MD , 4.32, , 342.54
S3, 22, MD , 3.54, , 0.32
S4, 22, MD , 4.32, , 0.54
S1, 33, MD , 5.32, , 0.43
S2, 33, MD , 11.54, , 0.65
S3, 33, MD , 22.5, , 0.324
S4, 33, MD , 45.89 , 0.32
S1, 44, MD , 3.53 , 3.32
S2, 44, MD , 4.5 , 0.322
S3, 44, MD , 43.65 , 45.78
S4, 44, MD, 43.54 , 0.321
以下是重新格式化数据的代码:
import csv
with open(r'C:\Users\testuser\Desktop\raw_inpuy.csv.csv','rb') as csvfile:
reader=csv.reader(csvfile, delimiter=',')
first=True
data={}
column= input("What column do you want to see: (x,y,z)")
for row in reader:
size = int(row[1])
column = float(row[3]) # the problem lies here. I know that the column header plus row # must match but I do not know how to code it
if not size in data:
data[size] = []
data[size].append(column)
writer = csv.writer(sys.stdout)
writer.writerow(["Size", "S1", "S2", "S3", "S4"])
for item in data:
row = [item]
row.extend(data[item])
writer.writerow(row)
将其输出到csv:
Size,S1,S2,S3,S4
33,5.32,11.54,22.5,45.89
44,3.53,4.5,43.65,43.54
22,0.022,4.32,3.54,4.32
这是正确的,但它只显示 time1 列,但如果我要求,我希望能够看到 time2 列。 还有一个我没有包含的time3列 即使我要求用户输入并将其声明为变量,它仍然只打印time1列,即使我要求time2列。
答案 0 :(得分:1)
你可以试试这个:(请注意,我必须从csv的第一列中删除空格和尾随逗号...还要注意在你的示例csv中留下了一些不一致的commans)
import io
from csv import DictReader
csvfile = io.StringIO('''Names,Size,State,time1,time2
S1, 22, MD , 0.022 , 523.324
S2, 22, MD , 4.32 , 342.54
S3, 22, MD , 3.54 , 0.32
S4, 22, MD , 4.32 , 0.54
S1, 33, MD , 5.32 , 0.43
S2, 33, MD , 11.54 , 0.65
S3, 33, MD , 22.5 , 0.324
S4, 33, MD , 45.89 , 0.32
S1, 44, MD , 3.53 , 3.32
S2, 44, MD , 4.5 , 0.322
S3, 44, MD , 43.65 , 45.78
S4, 44, MD, 43.54 , 0.321
''')
reader = DictReader(csvfile, delimiter=',')
first = True
data={}
col_name = input("What column do you want to see: (x,y,z)")
for row in reader:
# print(row)
size = int(row['Size'])
column = float(row[col_name])
if not size in data:
data[size] = []
data[size].append(column)
print(data)
输出(对于time1);
What column do you want to see: (x,y,z)time1
{33: [5.32, 11.54, 22.5, 45.89], 44: [3.53, 4.5, 43.65, 43.54],
22: [0.022, 4.32, 3.54, 4.32]}
<强>更新强>
然后你可以用
之类的东西写出来import io
from csv import DictWriter
out_csv = io.StringIO('''''')
fieldnames = ['Size', col_name]
writer = DictWriter(out_csv, fieldnames=fieldnames)
writer.writeheader()
for key, value in data.items():
row = {'Size': key, col_name: value}
print(row)
writer.writerow(row)
print(out_csv.getvalue())
此输出:
Size,time2
33,"[0.43, 0.65, 0.324, 0.32]"
44,"[3.32, 0.322, 45.78, 0.321]"
22,"[523.324, 342.54, 0.32, 0.54]"