Jun-04-2020, 11:14 AM
Greetings,
I have a csv file with an odd format. It consists of a number of 10 blocks.
The first line of the block has a "header" 'Grid-ref=X(int), Y(int)
Each of the nine rows, has 12 columns with spaces, not a ',' separator.
### Python used to read
for blocks in datareader:
print(blocks)
##PRINTS Nested List> seperator = '' not ','
#csv structure is:
['Grid-ref= 1', ' 148']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
['Grid-ref= 1', ' 311']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
['Grid-ref= 1', ' 312']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
#### Python used to read
for blocks in datareader:
for line in blocks:
print(line)
#### PRINTS the lines >> seperator = '' not ','
# csv structure:
Grid-ref= 1
148
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
Grid-ref= 1
311
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
Grid-ref= 1
312
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
The data structure is:
Block 1
(n = 10 rows) (n = 12 columns)
Jan Feb Mar Apr May Apr May Jun Jul Aug Sept Oct Nov Dec
Grid-ref= Int, int
1991 int int int int int int int int int int int int
1992 int int int int int int int int int int int int
1993 int int int int int int int int int int int int
1994 int int int int int int int int int int int int
1995 int int int int int int int int int int int int
1996 int int int int int int int int int int int int
1997 int int int int int int int int int int int int
1998 int int int int int int int int int int int int
1999 int int int int int int int int int int int int
2000 int int int int int int int int int int int int
I want to insert each block into a table.
I want to loop through each block of 10 rows + 12 columns and insert into (Data format ):
From Jan-1991 to Dec-2000
Index X(Int), Y(int) Jan-1991 Feb-1991 Mar-1991 … Dec-1991 Jan-1991 Feb-1991 Mar-1991 … Dec-1992 … Dec-2000
1 int int int int int int int int int int (n)int
2 int int int int int int int int int int (n)int
n int int int int int int int int int int (n)int
I have read the csv file but cannot loop through each block, and insert into the table??
Any assistance is appreciated!!
I have a csv file with an odd format. It consists of a number of 10 blocks.
The first line of the block has a "header" 'Grid-ref=X(int), Y(int)
Each of the nine rows, has 12 columns with spaces, not a ',' separator.
### Python used to read
for blocks in datareader:
print(blocks)
##PRINTS Nested List> seperator = '' not ','
#csv structure is:
['Grid-ref= 1', ' 148']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
[' 3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630']
['Grid-ref= 1', ' 311']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
[' 490 290 280 230 200 250 440 530 460 420 530 450']
['Grid-ref= 1', ' 312']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
[' 460 280 260 220 190 240 430 520 450 400 520 410']
#### Python used to read
for blocks in datareader:
for line in blocks:
print(line)
#### PRINTS the lines >> seperator = '' not ','
# csv structure:
Grid-ref= 1
148
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
3020 2820 3040 2880 1740 1360 980 990 1410 1770 2580 2630
Grid-ref= 1
311
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
490 290 280 230 200 250 440 530 460 420 530 450
Grid-ref= 1
312
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
460 280 260 220 190 240 430 520 450 400 520 410
The data structure is:
Block 1
(n = 10 rows) (n = 12 columns)
Jan Feb Mar Apr May Apr May Jun Jul Aug Sept Oct Nov Dec
Grid-ref= Int, int
1991 int int int int int int int int int int int int
1992 int int int int int int int int int int int int
1993 int int int int int int int int int int int int
1994 int int int int int int int int int int int int
1995 int int int int int int int int int int int int
1996 int int int int int int int int int int int int
1997 int int int int int int int int int int int int
1998 int int int int int int int int int int int int
1999 int int int int int int int int int int int int
2000 int int int int int int int int int int int int
I want to insert each block into a table.
I want to loop through each block of 10 rows + 12 columns and insert into (Data format ):
From Jan-1991 to Dec-2000
Index X(Int), Y(int) Jan-1991 Feb-1991 Mar-1991 … Dec-1991 Jan-1991 Feb-1991 Mar-1991 … Dec-1992 … Dec-2000
1 int int int int int int int int int int (n)int
2 int int int int int int int int int int (n)int
n int int int int int int int int int int (n)int
I have read the csv file but cannot loop through each block, and insert into the table??
Any assistance is appreciated!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import csv import os, sys from itertools import islice import re import pandas as pd ## sys.setdefaultencoding('utf-8') table = [] csvfile = open ( 'cru-ts-2-10-1991-2000-cutdown.csv' , 'r' ) datareader = csv.reader(csvfile) ### datareader = csv.reader(csvfile, delimiter=',') for blocks in datareader: #print(blocks) #### PRINTS Nested List >> seperator = '' not ',' #for i in blocks[0]: #print(i) for line in blocks: print (line) #### PRINTS the lines ## Prints the index, not the ##for line in range(10): ## print(line) df = pd.DataFrame(table) ##print(df) #### Empty table |