👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I"m trying to parse through a csv file and extract the data from only specific columns.
Example csv:
ID | Name | Address | City | State | Zip | Phone | OPEID | IPEDS |
10 | C... | 130 W.. | Mo.. | AL... | 3.. | 334.. | 01023 | 10063 |
I"m trying to capture only specific columns, say ID
, Name
, Zip
and Phone
.
Code I"ve looked at has led me to believe I can call the specific column by its corresponding number, so ie: Name
would correspond to 2
and iterating through each row using row[2]
would produce all the items in column 2. Only it doesn"t.
Here"s what I"ve done so far:
import sys, argparse, csv
from settings import *
# command arguments
parser = argparse.ArgumentParser(description="csv to postgres",
fromfile_prefix_chars="@" )
parser.add_argument("file", help="csv file to import", action="store")
args = parser.parse_args()
csv_file = args.file
# open csv file
with open(csv_file, "rb") as csvfile:
# get number of columns
for line in csvfile.readlines():
array = line.split(",")
first_item = array[0]
num_columns = len(array)
csvfile.seek(0)
reader = csv.reader(csvfile, delimiter=" ")
included_cols = [1, 2, 6, 7]
for row in reader:
content = list(row[i] for i in included_cols)
print content
and I"m expecting that this will print out only the specific columns I want for each row except it doesn"t, I get the last column only.
👻 Read also: what is the best laptop for engineering students?
Read specific columns from a csv file with csv module? __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
Read specific columns from a csv file with csv module? __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from Read specific columns from a csv file with csv module?, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Read specific columns from a csv file with csv module?
- Deutsch Read specific columns from a csv file with csv module?
- Français Read specific columns from a csv file with csv module?
- Español Read specific columns from a csv file with csv module?
- Türk Read specific columns from a csv file with csv module?
- Русский Read specific columns from a csv file with csv module?
- Português Read specific columns from a csv file with csv module?
- Polski Read specific columns from a csv file with csv module?
- Nederlandse Read specific columns from a csv file with csv module?
- 中文 Read specific columns from a csv file with csv module?
- 한국어 Read specific columns from a csv file with csv module?
- 日本語 Read specific columns from a csv file with csv module?
- हिन्दी Read specific columns from a csv file with csv module?
Paris | 2023-04-01
Thanks for explaining! I was stuck with Read specific columns from a csv file with csv module? for some hours, finally got it done 🤗. Will get back tomorrow with feedback
California | 2023-04-01
Maybe there are another answers? What Read specific columns from a csv file with csv module? exactly means?. I am just not quite sure it is the best method
Milan | 2023-04-01
readline Python module is always a bit confusing 😭 Read specific columns from a csv file with csv module? is not the only problem I encountered. I just hope that will not emerge anymore