Python Forum
Generate Multiple sql Files With csv inputs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate Multiple sql Files With csv inputs
#7
(Aug-19-2021, 07:13 PM)deanhystad Wrote: I don't understand your question. Could you provide an example of real data?

I have the data in the .csv file as below. I have attached the .csv file in the first post of this thread.
DB SCH TAB COL
DEV SCOTT EMP EMPNO
DEV SCOTT EMP ENAME
DEV SCOTT EMP DEPTNO
DEV SCOTT DEPT DEPTNO
DEV SCOTT DEPT DNAME
DEV SCOTT DEPT LOC

And I have wrote the below code and almost got the output except a minor one in the print

df = pd.read_csv("view.csv")
#print(df)
df1=df.groupby(['DB','SCH','TAB'])['COL'].apply(','.join)
# groupby_db = df.groupby("DB")
# print(groupby_db)
# grouped_lists = groupby_db["COL"].apply(list)
# print(grouped_lists)
#print(df1)
df2 = df1.reset_index(name = 'COLS')
print (df2)
for ind,row in df2.iterrows() :
    if ind > len(df2):
        break
    else:
        f=open(row["TAB"]+ '_V.txt','w')
        f.write("CREATE OR REPLACE VIEW " + row["DB"] + "." + row["SCH"]+ "." + row["TAB"]+ "_V \nAS\nSELECT\n"+row["COLS"] + "\nFROM\n"+ row["DB"] + "." + row["SCH"]+ "." + row["TAB"]+";")
        f.close()
        ind+=1
    print("CREATE OR REPLACE VIEW " + row["DB"] + "." + row["SCH"]+ "." + row["TAB"]+ "_V \nAS\nSELECT\n"+row["COLS"] + "\nFROM\n"+ row["DB"] + "." + row["SCH"]+ "." + row["TAB"]+";")
All I want to get is
CREATE OR REPLACE VIEW DEV.SCOTT.EMP_V
AS
SELECT
EMPNO,
ENAME,
DEPTNO
FROM
DEV.SCOTT.EMP;

What I m getting is

CREATE OR REPLACE VIEW DEV.SCOTT.EMP_V
AS
SELECT
EMPNO,ENAME,DEPTNO
FROM
DEV.SCOTT.EMP;

All I need is this minor change to my code or else any far better approach.

Attached Files

.csv   view.csv (Size: 143 bytes / Downloads: 76)
Reply


Messages In This Thread
RE: Generate Multiple sql Files With csv inputs - by vkomarag - Aug-20-2021, 03:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to generating multiple json files using python script dzgn989 4 253 May-10-2024, 03:09 PM
Last Post: deanhystad
  python convert multiple files to multiple lists MCL169 6 1,675 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Multiple variable inputs when only one is called for ChrisDall 2 534 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  splitting file into multiple files by searching for string AlphaInc 2 973 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,246 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 999 Feb-15-2023, 05:34 PM
Last Post: zsousa
  Find duplicate files in multiple directories Pavel_47 9 3,266 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 2,146 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Search multiple CSV files for a string or strings cubangt 7 8,238 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  Rename part of filename in multiple files atomxkai 7 7,490 Feb-18-2022, 10:03 PM
Last Post: atomxkai

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020