Answer the question
In order to leave comments, you need to log in
How to parse and load data into a database?
Given:
1. csv file, but relative to the curve. The data is present there in the format
field1, field2, field3, field4
data1, data2, data3, data4
field1, field2, field3, field4, field5
data1, data2, data3, data4, data5
some_line_with_random_data
!^*@$!( The
file describes the data of several tables at once, along the way there are blotches of just text strings of the subtotal type
2.
It is necessary to parse the data and store it in the database. 1. I
sketched
a function that runs through the entire file and extracts the lines I need, returns a list of the format ['data1,data2,data3\n', 'data4,data5,data6\n', ]
This is how the function looks like
def parsecsv_equities(filename):
f = open(filename, "r")
trade_data_list = []
for line in f:
if 'Trades,Data,Order,Equity' in line:
trade_data_list.append(line)
f.close()
return trade_data_list
def parsecsv_futures(filename):
f = open(filename, "r")
trade_data_list = []
for line in f:
if 'Trades,Data,Order,Futures' in line:
trade_data_list.append(line)
f.close()
return trade_data_list
import pyodbc
def dbconnect(str):
conn = pyodbc.connect('Driver={SQL Server};'
'Server=1.1.1.1;'
'Database=Reports;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute(str)
for row in cursor:
print(row)
Answer the question
In order to leave comments, you need to log in
Can it is better to look at once towards Data Transformation Service (type an integral part of MS SQL)?
And you look at PIVOT
Either on the official Microsoft
documentation
And in general, I would get a normal TABLE, and I would already break it into the necessary parts and transfer it to a new database.
A would have received a transposition, as in the above functions.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question