I have a problem. I have a csv that stores data about checks in a private restaurant . Not CC's or anything, just table number, items, server number, blah. It repeats for every check a four lines that start with 1,4,5,6,7. I have a interface that picks up this csv and reads line four to help billing guests. It was written in VB, it will not be re-written by the programmers. I need the data from line 7 to be in the line four position. I was looking over the csv reader and writer objects, but I can't wrap my head around this. Any ideas? Thank you so much in advance.

8 Years
Discussion Span
Last Post by zachabesh

Huh? Not exactly sure what you mean, but if the csv writer confuses you, just write to it like this:

csvfile = open('myrestaurant.csv','w')

As far as I know, csv files are just text files of comma delimited strings. one line = one row.


Yes zachabest is correct. There is nothing special about a csv file. It is literally a text file that uses commas to delimit, or separate, each data cell of a row. Each new line is therefore a new row.

I've found that using the csv modules are overkill and it is much easier to simply read the file as standard text and use common PYthon operations like split(',') and ','.join() to manipulate your data.


First off, thanks for the responses. The issue isn't as much writing a line in the same format. The line i need is already in the file, I need to copy it and delete a existing line, and replace the deleted line with the line I have copied.



I would copy line 7, delete line 4, and replace it with line seven. Also, this format repeats for every check that day, so it would be for all sevens copy, for all fours, replace with seven.

Thanks for any help.


My personal preference would be using a dictionary since it would be indexed on the number, but there are several ways to do this along the lines of the following. Add the recs to a dictionary. When a 7 is found, send the dictionary to a function to process. If this isn't what you want, then post your code and we will try to correct it for you.

##   print (or write to a file) the existing dictionary of records in a "replace 4 with 7" order
def process_list(rec_dic):
   write_order = (8, 9, 1, 2, 3, 7, 5, 6, 7)   ## note "7" prints twice here
   print "\nwriting records to the file in this order"
   for key in write_order:
      if key in rec_dic:
         print "     ", rec_dic[key]

Instead of changing the file, why don't you just change the way you read it?

as in, your function that processes the line would now read


instead of


Python doesn't care what's in the file if you never use it.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.