1

By reference to this question: How to extract and copy lines from csv file to another csv file in python? the result That I have after executing this code based on this array:

frame.number    frame.len   frame.cap_len   frame.Type  
  1               100           100           ICMP_tt   
  2                64            64           UDP   
  3               100           100           ICMP_tt   
  4                87            64           ICMP_nn
  5               100           100           ICMP_tt   
  6                87            64           ICMP_nn
  7               100           100           ICMP_tt   
  8                87            64           ICMP_nn
  9                87            64           ICMP_nn

This is the code:

# read
data = []
with open('test.csv', 'r') as f:
    f_csv = csv.reader(f)
    # header = next(f_csv)
    for row in f_csv:
        data.append(row)

# write
with open('newtest.csv', 'w+') as f:
    writer = csv.writer(f)
    for i in range(int(len(data) * 30 / 100)):
        writer.writerow(data[i])

This is the result in the newtest.csv file:

 frame.number    frame.len   frame.cap_len   frame.Type 
     empty line....
      1               100           100           ICMP_tt 
   empty line....
      2                64            64           UDP 

However, I hope that the result looks like this:

 frame.number    frame.len   frame.cap_len   frame.Type 
    1               100           100           ICMP_tt 
    2                64            64           UDP 

The test.csv file stil the same I mean that the two lines copied are not deleted. that means that I want to have:

  frame.number    frame.len   frame.cap_len   frame.Type  
      3               100           100           ICMP_tt   
      4                87            64           ICMP_nn
      5               100           100           ICMP_tt   
      6                87            64           ICMP_nn
      7               100           100           ICMP_tt   
      8                87            64           ICMP_nn
      9                87            64           ICMP_nn

I hope that you can help me please.

tierrytestu
  • 119
  • 4
  • 12
  • 1
    does your actual csv look like that? or commas as a separator? or you use a spreadsheet to input the value? because csv shouldn't look like that, it should look like `frame.number,frame.len,frame.cap_len,frame.Type` and is separated by commas. – Abhishta Gatya Oct 22 '17 at 10:30
  • Using python2? Then open it in binary mode. – cs95 Oct 22 '17 at 10:32
  • @AbhishtaGatya yes they are sepereated by comma like you have said , I put them like an array to be more clear just that. – tierrytestu Oct 22 '17 at 10:35
  • @cᴏʟᴅsᴘᴇᴇᴅ You have marked my question but it is not the case , Because that solution does not give me the correct solution that I wait for – tierrytestu Oct 22 '17 at 10:37
  • I know for a fact that you have pandas, so why not use that? – cs95 Oct 22 '17 at 10:37
  • yes I use it to make some modification, but I don't know how to use it for moving data. – tierrytestu Oct 22 '17 at 10:40
  • Then I don't want to have a binary file as a result , because it will make its analysis very hard to do – tierrytestu Oct 22 '17 at 10:45
  • @cᴏʟᴅsᴘᴇᴇᴅ the answer that you give me is no more how to extract lines from csv file then copying them in another csv file , it is about delimeters, I don't know why did you put my question as duplicated, I would be very grateful if you could give me your opinion ? – tierrytestu Oct 22 '17 at 11:15
  • I marked your question duplicate because your output has superfluous newlines, when you don't want it to. I asked you what your python version was, you haven't responded either. This happens to be a classic python2 problem. – cs95 Oct 22 '17 at 11:16
  • It was just a set seconds, you have put my question as duplicated before having my answer, any way I have python2, and the problem is how to move my lines not just copy them, so I don't find any solution abour my problem in those answers. https://stackoverflow.com/questions/3191528/csv-in-python-adding-an-extra-carriage-return – tierrytestu Oct 22 '17 at 11:23
  • Did you try this? `with open('newtest.csv', 'rb') as f:` and `with open('newtest.csv', 'wb+') as f:` – cs95 Oct 22 '17 at 11:34
  • @cᴏʟᴅsᴘᴇᴇᴅ yes it is the same thing that I did in my code that I am posting it now – tierrytestu Oct 22 '17 at 11:40

1 Answers1

0

To do the read and write separately in Python 2.x, you could use the following approach:

import csv

with open('test.csv', 'rb') as f_input:
    data = list(csv.reader(f_input))

with open('newtest.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerows(data)

This would mean data holds all of the rows as a list of lists. Make sure the files are opened in binary mode for both reading and writing. If this is not done, you will get empty lines.

To do this one row at at time (useful if the CSV file is too large for memory), you could do the following:

with open('test.csv', 'rb') as f_input, open('newtest.csv', 'wb') as f_output:
    csv_input = csv.reader(f_input)
    csv_output = csv.writer(f_output)    

    for row in csv_input:
        csv_output.writerow(row)

Or even:

with open('test.csv', 'rb') as f_input, open('newtest.csv', 'wb') as f_output:
    csv_input = csv.reader(f_input)
    csv_output = csv.writer(f_output)    
    csv_output.writerows(csv_input)
Martin Evans
  • 45,791
  • 17
  • 81
  • 97