Question

Psycopg2 Python and PostgreSQL If I am given a text file called test.txt that contains the...

Psycopg2 Python and PostgreSQL

If I am given a text file called test.txt that contains the name of various tables

*test.txt and its contents*

testTable

Students

Teachers

Using Psycopg2 (Python and PostgreSQL), how would you read the text file and output the number of rows each table contains?

Homework Answers

Answer #1

You can do this task as two parts.

Part 1 : Read the table names from file

Part 2 : find the row count in each table

code :

For part 1 : open the file and read the content to a variable:

f = open("test.txt")
data = f.read().split("\n")
f.close()

Now the variable data contains the list of table names.

Par2 : As you dosent specify the database name, I hope you had already made a connection to the database.

and let variable "conn" be the conncetion reference to the database.

Then create a cursor object using that conn variable

as


#Creating a cursor object using the cursor() method
cursor = conn.cursor()
for i in data:
  querry = "SELECT * from "+i
  #Retrieving data
  cursor.execute(querry)

  #Fetching all row from the table
  result = cursor.fetchall();
  print("The table named ",i," contains ",len(result), + " rows.")

#Closing the connection
conn.close()
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
I need python code for this. Write a program that inputs a text file. The program...
I need python code for this. Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'. The input file can contain one or more sentences, or be a multiline list of words. An example input file is shown below: example.txt the quick brown fox jumps over the lazy dog An example of the program's output...
The following code to run as the described program on python. The extra test file isn't...
The following code to run as the described program on python. The extra test file isn't included here, assume it is a text file named "TXT" with a series of numbers for this purpose. In this lab you will need to take a test file Your program must include: Write a python program to open the test file provided. You will read in the numbers contained in the file and provide a total for the numbers as well as the...
Python problem: write a main function that ask user for 2 file name, open files and...
Python problem: write a main function that ask user for 2 file name, open files and read the 1st line of each and compare them using hamming function(below is the hamming function I wrote). Just assume the fist line of each file only contains 0s and 1s, such as 0100111101. The main function may have to do some extra work to remove newline characters or other whitespace from the text read from each file. This is hamming function that need...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report the total number of words (as an integer) and the average word length (as an un-rounded number). For example, suppose the file tobe.txt contains the following text: To be or not to be, that is the...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Write a python code to print (any given file) file as a csv format by using...
Write a python code to print (any given file) file as a csv format by using MRJob only. code must be able to run stand-alone MRJob application. For submission to your work: Your final hand should be a single file name BDM.py that takes exactly one arguments for the input path. output will be handled through redirection. Sample run: python BDM.py sample.csv > output.csv
Writing a program in Python that reads a text file and organizes the words in the...
Writing a program in Python that reads a text file and organizes the words in the file into a list without repeating words and in all lowercase. Here is what I have #This program takes a user input file name and returns each word in a list #and how many different words are in the program. while True:   #While loop to loop program     words = 0     #list1 = ['Programmers','add','an','operating','system','and','set','of','applications','to','the','hardware',          # 'we','end','up','with','a','Personal','Digital','Assistant','that','is','quite','helpful','capable',           #'helping','us','do','many','different','things']        try:        ...
You will utilize your Python environment to derive structure from unstructured data. You will utilize the...
You will utilize your Python environment to derive structure from unstructured data. You will utilize the given data set. Using this data set, you will create a text analytics Python application that extracts themes from each comment using term frequency–inverse document frequency (TF–IDF) or simple word counts. For the deliverable, provide your Python file and a .csv with your results added as a column to the original data set. * I am unable to provide the data set itself, as...
I am having some trouble trying to create a linked list from a text file input...
I am having some trouble trying to create a linked list from a text file input from user The three lines respectively represent a long integer ID of a person, the name of the person, and an integer ranging from 0 to 5 indicating the threat level posed by a person. 389114 Paul Bunion 5 399012 John Doe 0 685015 Johnny Appleseed 3 179318 Tom Sawyer 2 284139 Ebenezer Scrooge 5 The full connection is: Paul Bunion -> John Doe...
How would I solve these python problems? A) File Display Download the file from here named...
How would I solve these python problems? A) File Display Download the file from here named numbers.txt . Write a program that displays all of the numbers in the file. numbers.txt contains 22 14 -99 AB B) Error Check Float Input When you want an int input you can check the input using the string isdigit() method. However, there is no comparable check for float. Write a program that asks the user to enter a float and uses a try-except...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT