-1

I am trying to write a specific portion of data from a .txt file to a different .txt file for later use.

Code is below.

file = open(path, newline='')
reader = csv.reader(file)

header = next(reader)
data = [row for row in reader]

#read only cartesian points to new text file
f = open("Cartesian Points.txt", "w+")
#create a range from the first cartesian point 75054 to the last 1048576
for i in range(data[75054],data[1048576],1):
    f.write(data[i])
f.close()

My idea is to parse the original file completely, then create a range for the cartesian points and write this to a different .txt file for later use.

However when writing the data I am receiving an error

    for i in range(data[75054],data[1048576],1):
IndexError: list index out of range

I am confused as I know that the data ranges from cell 75054 to 1048576 and it should simply write that data to the new .txt file. I don't see why the data would continue on past the specified range.

Dharman
  • 30,962
  • 25
  • 85
  • 135
John
  • 25
  • 9
  • Did you try to print `len(data)`? Do you really get a value bigger than 1048576? – Matthias Jul 05 '22 at 11:37
  • I suggest *print(len(data))* before entering the loop. You'll probably find the output very interesting – DarkKnight Jul 05 '22 at 11:37
  • Are you sure that you didn't want `range(75054, 1048576,1)` ? – nonDucor Jul 05 '22 at 11:38
  • 1
    Off-by-one error? https://en.wikipedia.org/wiki/Off-by-one_error – Peter Wood Jul 05 '22 at 11:38
  • as suggested use print or even logging (or debug your variable step by step if your IDE support it, PyCharm does) to see the actual value of data[] – NicoCaldo Jul 05 '22 at 11:38
  • printing that gives me 1048575, however when changing the end of the range to `data[1048575]` I still receive the same error. – John Jul 05 '22 at 11:40
  • @nonDucor don't I need to reference the data by using `data[some arbitrary number]` to properly start and end the range? – John Jul 05 '22 at 11:41
  • 1
    If you want to write in the output the rows from 75054 to 1048576, that's what should be on your range (because these are the values of `i`, which is an index). When you do `f.write(data[i])` then you'll get the value of the row to write in the file. – nonDucor Jul 05 '22 at 11:43

1 Answers1

0

The code for i in range(data[75054],data[1048576],1) actually takes the content of data at position 75054 as integer number for the range() function.

From your question it seems you want to cycle inside the range 75054 <-> 1048576. This can be achived with for i in range(75054,1048576,1)

With the code

for i in range(75054,1048576):
    f.write(data[i])

You cycle from 75054 to 1048576 and write into the file (object f) the content of data[i] at the position i (which is an integer inside the cycle)

In the example above the 1 at the end in range() has been removed because it's set to 1 as default by Python itself

Some documentation about range(). You may also want to check lists in Python

NicoCaldo
  • 1,171
  • 13
  • 25