-1

I just found out it's not possible to write to a specific line in a csv file (only the end).

I have just come across another obstacle that I'm having trouble tackling, which is reading from a specific line in a csv file. One way I have found to accomplish this is:

with open('file.csv',newline = '') as csvfile:
    spamreader = csv.reader(csvfile,delimiter=',',quotechar = '"')
    lines = []
    for row in spamreader:
        lines.append(row)

print('What line do you want to read from?')
line = lines[int(input())-1] #I think the -1 is right. since lists start at 0

However, I believe that this might be a slightly inefficient way to do this, since the more rows in the list "lines", the more RAM the program would be using.

Could someone tell me if this is actually an efficient way of doing this? Otherwise, I will just go with this.

Is there any way that I can do something like this?

spamreader.readRow(5) #I just made this up, but is there a similar function?

This is the page that I've been using, it's possible I skipped over it. https://docs.python.org/3/library/csv.html

Also, I'm not very advanced in programming, so if there is an advanced answer, can you try to keep the explanations fairly simple?

j-i-l
  • 10,281
  • 3
  • 53
  • 70
Kyle Me
  • 336
  • 2
  • 5
  • 13
  • possible duplicate of [Reading specific lines only (Python)](http://stackoverflow.com/questions/2081836/reading-specific-lines-only-python) or [python: how to jump to a particular line in a huge text file?](http://stackoverflow.com/questions/620367/python-how-to-jump-to-a-particular-line-in-a-huge-text-file) – leo Aug 13 '14 at 20:46
  • See also http://stackoverflow.com/questions/19224689/python-reading-specific-lines-from-csv-using-list-comprehension – leo Aug 13 '14 at 20:48
  • I looked at both of your solutions, and I tried every answer on each page, and none of them worked. Although this may cause for a long comment, I disagree that my question has already been answered in your links. (I can be wrong!) First: I tried using the enumerate method. I wasn't sure exactly how to get this to work. The top answer said "If i == line_number: #line_number -1"... Not really sure what to do with that. Second: I tried using linecache, in the fashion of "line = linecache.getline('file.csv',int(input())", but this raised numerous errors. – Kyle Me Aug 13 '14 at 21:29
  • Third: I tried using "selection = [row for row in header if row[2] == 'a'] I don't think that this will work because I would like to give a line number, and jump to that line. The reason this doesn't work is because I do not believe there is a way to extract the line number from the row. If you look at the example that the OP gave, he was creating a list of possible selected rows if a specific "cell" contained certain information. – Kyle Me Aug 13 '14 at 21:32

1 Answers1

1

If you want to read starting from line 123:

for _ in range(122): 
    spamreader.next()
for row in spamreader:
    ...

With Python 3 it seems to be

    next(spamreader)

One can also navigate in the file by moving the cursor to a specific byte using find and seek.

JulienD
  • 7,102
  • 9
  • 50
  • 84
  • This gives me the error: '_csv.reader' object has no attribute 'next' – Kyle Me Aug 13 '14 at 21:57
  • 1
    I did not try with Python 3, try this: `next(spamreader)` (https://docs.python.org/3/library/csv.html#csv.csvreader.__next__). For me (Python 2.7, csv 1.0), it works. What does `dir(spamreader)` return for you? – JulienD Aug 14 '14 at 11:04
  • Darn, I can't believe I didn't catch that. Thank you very much, you have solved my problem :). The way I managed to get this to work was: `for row in range(int(input()): chosenRow = next(spamreader)` And then I can do whatever I want with chosenRow. Thank you very much. – Kyle Me Aug 14 '14 at 19:30
  • 1
    Great. Then accept the answer :) especially since it is better than the ones in the posts advised above... – JulienD Aug 14 '14 at 20:06