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.

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')
csvfile.write('Server,Table,Items,Bill\n')
csvfile.write('Gary,10,4,$40')
csvfile.close()

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.

Format:

1,02,555,22.22
4,5,8,29,5
5,5,6,88,5
6,558,9,22.22
7,8,2.50,22,3,64

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

do(line7)

instead of

do(line4)

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

-Zach

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.