I have an array LiveTick = ['ted3m index','US0003m index','USGG3m index']
and I am reading a CSV file book1.csv
. I have to find the row which contains the values in csv.
For example, 15th row will contain ted3m index 500 | 600
ted3m index 500 | 600
and 20th row will contain US0003m index 800 | 900
US0003m index 800 | 900
and likewise.
I then have to get the values contained in the row and parse it for each value contained in array LiveTick
. How do I proceed? Below is my sample code:
with open('C:\\blp\\book1.csv', 'r') as f:
reader = csv.reader(f, delimiter=',')
writer = csv.writer(outf)
for row in reader:
for list in LiveTick:
if list in row:
print ('Found: {}'.format(row))
You can use pandas, it's pretty fast and will do all reading, writing and filtering job for you out of the box:
import pandas as pd
df = pd.read_csv('C:\\blp\\book1.csv')
filtered_df = df[df['your_column_name'].isin(LiveTick)]
# now you can save it
filtered_df.to_csv('C:\\blp\\book_filtered.csv')
You have the right idea, but there are a few improvements you can make:
for
loop which doesn't short-circuit, use any
to compare the first column to multiple values. print
. This is memory-efficient, as you hold in memory only one line at any one time. outf
as an open
object in your with
statement. list
. Use another identifier, eg i
, for elements in LiveTick
. Here's a demo:
with open('in.csv', 'r') as f, open('out.csv', 'wb', newline='') as outf:
reader = csv.reader(f, delimiter=',')
writer = csv.writer(outf, delimiter=',')
for row in reader:
if any(i in row[0] for i in LiveTick):
writer.writerow(row)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.