1

How can I output the data I've formatted to a csv file? I believe it's possible to write each line as I clean them but I'm not sure how. The basic goal I'm trying to achieve is to iterate through all rows of the csv and change only the items in the rows that meet the criteria of the for loop. Then output all the changed and unchanged rows back out to another 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)
9
  • To write a csv file, simply write each line as a comma separated string. You don't need any extra package. Open a file in append mode, and append each line (row) when you have processed them. Commented Feb 27, 2016 at 17:39
  • @warmoverflow I'm not sure how to do that, can you explain? Commented Feb 27, 2016 at 17:41
  • before first for open file to write . with open('target_file', 'w') as tf : . Then repeat tf.write(result) inside each if block. Commented Feb 27, 2016 at 17:41
  • @minatverma won't that only write the results of the changed lines? Commented Feb 27, 2016 at 17:46
  • add an extra else at last to write non matched line . Commented Feb 27, 2016 at 17:47

2 Answers 2

2

One simple way to accomplish what you are trying to do is to read your file as a list, make your changes, and then write the list to a new file:

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)
Sign up to request clarification or add additional context in comments.

2 Comments

that files comes out empty
if you are able to include a snippet of your input file in your question, I may be able to provide additional help.
1
newfile = open("test.csv", "w")
for row in readerObj:
  newrow = []
  for item in row:
    if " TB" in item:
      item = item.replace(" TB", "")
      item = re.sub('[^0-9]', '', item)
      item = float(item) * 1024
      item = round(item, 2)
    elif " MB" in item:
      item =  item.replace(" MB", "")
      item = re.sub('[^0-9]', '', item)
      item = float(item) / 1000
      item = round(item, 2)
    elif " GB" in item:
      item = item.replace(" GB", "")
      item = re.sub('[^0-9]', '', item)
      item = float(item)
      item = round(item, 2)
    newrow.append(str(item))
  newfile.write(','.join(newrow) + '\n')
newfile.close()
fileToClean.close()

7 Comments

Also, as a good practice, always remember to either close the file manually, or use with open(file, 'w') as f:
This produces an empty output file.
I have tested my code and it works. Please post a sample of your input data so I can test again.
My mistake, the read file had been cleared prior to any formatting. You solution works, thank you very much!
how would I iterate over the reader object again prior to writing to the new file?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.