简体   繁体   中英

Using Python for adding column in a CSV file

I have csv file (inputFile) like below:

Temperature,2,3
Temperature,5,6
Pressure,11,14,45
Pressure,13,23,16
Humidity,21,24,25
Humidity,27,28,26

and I want to write it into another file(outputFile), but in the following format:

Temperature,2,3,Pressure,11,14,45,Humidity,21,24,25
Temperature,5,6,Pressure,13,23,16,Humidity,27,28,26

I have tried following Python code:

 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)]) 

Which is giving the output in the follwoing format:

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

Please help!

Binary mode is missing.

To increase readability I suggest to separate read, change data and write because you have to read the whole input file before writing.

Example (without error handling):

  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 ()

Try opening the file as wb rather than w .

This may only apply if you are running on Windows. It's an issue with line seperators in file handles.

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)

If you could do anything with the way the inputFile.csv is written it would make life for you much easier. Either way here is an pandas alternative that does solve your problem.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM