我有csv文件(inputFile),如下所示:
Temperature,2,3
Temperature,5,6
Pressure,11,14,45
Pressure,13,23,16
Humidity,21,24,25
Humidity,27,28,26
我希望将其写入另一个文件(outputFile),但格式如下:
Temperature,2,3,Pressure,11,14,45,Humidity,21,24,25
Temperature,5,6,Pressure,13,23,16,Humidity,27,28,26
我尝试过以下Python代码:
with open('inputFile.csv','r') as csvinput:
with open('outputFile.csv','w') as csvoutput:
writer = csv.writer(csvoutput, delimiter= ',')
writer = csv.writer(csvoutput)
for row in csv.reader(csvinput):
if (row[0] == "Pressure" or row[0] == "Humidity"):
type =row[0]
Value = row[1])
writer.writerow(row + [np.asarray(type)] + [np.asarray(Value)])
以下列格式提供输出:
Temperature,2,3,Humidity,27
Temperature,5,6,Humidity,27
Temperature,8,9,Humidity,27
Pressure,11,14,45,Pressure,11
Pressure,13,23,16,Pressure,13
Humidity,21,24,25,Humidity,21
Humidity,27,28,26,Humidity,27
请帮忙!
答案 0 :(得分:3)
缺少二进制模式。
为了提高可读性,我建议将读取,更改数据和写入分开,因为您必须在写入之前读取整个输入文件。
示例(没有错误处理):
import csv
f = open('inputFile.csv','rb')
reader = csv.reader(f)
data = {}
keys = set ()
for row in reader :
key = row [0]
data.setdefault (key, []).append (row)
f.close ()
odata = []
for (t, p, h) in zip (data ["Temperature"], data ["Pressure"], data ["Humidity"]) :
odata.append (t + p + h)
g = open('outputFile.csv','wb')
writer = csv.writer (g)
writer.writerows (odata)
g.close ()
答案 1 :(得分:0)
尝试将文件打开为wb
而不是w
。
这可能仅适用于在Windows上运行的情况。这是文件句柄中行分隔符的问题。
答案 2 :(得分:0)
import csv
with open('inputFile.csv','r') as csvinput:
with open('outputFile.csv','w') as csvoutput:
writer = csv.writer(csvoutput, delimiter=',')
types = ('temperature', 'pressure', 'humidity')
data = {key: [] for key in types}
for row in csv.reader(csvinput):
data[row[0].lower()].append(row[1:])
for entry_no in range(len(data['temperature'])):
row = []
for key in types:
row.extend([key.title()]+data[key][entry_no])
writer.writerow(row)
答案 3 :(得分:0)
如果你可以用inputFile.csv的编写方式做任何事情,它会让你的生活更轻松。无论哪种方式,这是一个解决您的问题的熊猫替代品。
import pandas as pd
df = pd.read_csv('inputfile.csv', names=['type', 'val1', 'val2', 'val3'])
df = df.T
a = range(0, len(df.columns))
rows = [a[::2], a[1::2]]
dic = {}
for i in range(0, 2):
dic[i] = [df[df.columns[j]].tolist() for j in rows[i]]
dic[i] = [j for x in dic[i] for j in x]
dic[i] = [x for x in dic[i] if str(x) != 'nan']
df1 = pd.DataFrame(dic)
df1.T.to_csv('outputFile.csv', index=False, header=False)