-1

i have written code for continues update of data from .csv file ..in this code i have 31 column and 6 rows ..here 1)i dont no how to converte into 2D form and after converting into 2D i want to read my data in column wise in table(how to create table in python for storing the element one after the other column wise )..Can anyone tell me how to read the row[1]column[1] data?..please can anyone help me to solve this problem..

def animate(i):
     filename = "tst.csv"
     fields = [] 
     rows = []
     Nrow = 0
     Ncol = 0
     cnt_row = 0
     cnt_col = 0

     with open(filename, 'r') as csvfile:
          csvreader = csv.reader(csvfile,delimiter = ",")
          data = list(csvreader)
          Nrow = len(data)
          for row in range(len(data)):
               for col in range(len(data[row])):
                    Ncol = data[row][col].split('.')
                    print(Ncol)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()

   Column1  Column2
0     4        8
1     6        9

i am expecting the result like this So that i can read my data in column wise

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • This answer seems to relate: https://stackoverflow.com/questions/24662571/python-import-csv-to-list – Atcrank May 31 '19 at 02:25

1 Answers1

0

I sugest you to use pandas to interact with tables (csv, excel, etc.) All your code above could be done in a few lines with pandas:

import pandas as pd

table = pd.read_csv('tst.csv')

# you can access data easly this way:
table['Columns1'][0]

# Or convert it to array:
table.values[0,0]

# and can easly add data and overwrite the old data:
table.append({'Column1':5,
           'Column1':9}, ignore_index=True)
table.to_csv('tst.csv', index=False)
Bruno Aquino
  • 103
  • 10
  • During handling of the above exception, another exception occurred: File "pandas\_libs\hashtable_class_helper.pxi", line 1601, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\_libs\hashtable_class_helper.pxi", line 1608, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'Columns1' i am getting error like this – mallamma Reddy May 31 '19 at 04:01
  • Seems that you dont have a column called Columns1. The index must be the name of the column, case sensitive. Try table.columns or tablet.head() – Bruno Aquino May 31 '19 at 11:57
  • Thank you ... Great! That was what i needed! – mallamma Reddy Jun 12 '19 at 13:45