Home > front end >  Python CSV Module (.cell)
Python CSV Module (.cell)

Time:01-13

I want to sort (to make a table) my data's from a csv-file. Now I have a template with the same thing I want to make, but with the openpyxl-library instead of the csv-library, which I use. In the openpyxl-library you can use ".cell" to speak to only to one cell. How can I do this with csv? Example with openpyxl

This is my current code for csv (file is the variable for my file, and .cell doesn't works):

csvreader = csv.reader(file)

column = []
column = next(csvreader)
for x in range(1, csvreader.line_num 1):
name = csvreader.delimiter(1, x).value
sqltype = ("INTEGER" if type(csvreader.delimiter(2, x).value) == int else "NVARCHAR(255)")
print(f'"{name}" {sqltype},')

CodePudding user response:

there is no cells in CSV so what you can do is loop through the file line by line.

so your code will look like:

  1. for loop going line by line,
  2. for each line split it by commas

for 2. now you have an array that has each value of the CSV, and the "cell" will be the array value so splitterLine[5] would be cell 6.

if you include the code any code that you started I could help you out more.

CodePudding user response:

My problem is solved: It's really simple, because with the CSV-Library you easily can make a Array with a csv-list. The Solution:

columns = []
for x in range(0, 10 1):
name = header[x]
sqltype = ("INTEGER" if type(header[x]) == int else "NVARCHAR(255)")
print(f'"{name}" {sqltype},')
columns.append(name)

How to make an array:

header = []
header = next(csvreader)
header

Variable csvreader(file is the variable from my imported file):

csvreader = csv.reader(file, delimiter=";")
  •  Tags:  
  • Related