Hi I would like to get stock prices data in an csv-file. I can read the stock prices but can't see them as a csv file.
I have searched for an soultion but couldn't find it.
from alpha_vantage.timeseries import TimeSeries
#my api server
ts = TimeSeries(key='YOUR_API_KEY',output_format='csv')
data = ts.get_intraday(symbol='AAPL',interval='1min', outputsize='compact')
print(data)
#(<_csv.reader object at 0x0F461370>, None)
How can I save the data now as an csv-file to work with it?
Niklas
You have a csv reader object. You can either iterate it or use list
Ex:
print(list(data))
or
for row in data:
print(row)
To write CSV to file use:
import csv
with open("outfile.csv", "w") as outfile:
writer = csv.writer(outfile)
writer.writerows(data)
You can use Python Pandas
Pandas
It has many features, like the ones you need, reading from dataframe or exporting to CSV. You can do much more things using the Pandas DataFrame.
DataFrame.to_csv
Related
Data from datadog
I am looking for some assistance reading this data from Datadog, I am reading it from the downloaded cvs. Wants to read in python so that create an application for the reading the same on regular intervals.
I have tried reading the data like below
import pandas as pd
fileload = pd.read_csv("DataSource/extract-2023-02-02T19_10_32.790Z.csv")
print(fileload)
fileload1 = pd.read_csv("DataSource/extract-2023-02-02T19_11_05.899Z.csv")
final = pd.concat([fileload, fileload1])
print(final)````
import csv
with open("DataSource/extract-2023-02-02T19_10_32.790Z.csv", 'r' ) as file:
csvread = csv.reader(file)
for i in file:
print(i)
a = pd.DataFrame([csvread])
print(type(a))
My expectation is that i can pick the last column with the all the data in the above format and further give column names to it. and then analyse data applying some aggregations on top.
Please assist
Have you tried:
final[["final_column_name"]]
final['New_col_name'] = ...
I have this code to scrape the results from google. If I have a list of terms I need to search in Excel/Csv format, how can I write the code to
After import the excel file, search each row values and print out the results for that row.
Repeat for the next row value in the Excel file.
Here's my code. Please help with any solution you can think of
For example my Excel file just have 1 column and 3 values as below:
List to search
Defuse
Commercial
Ecommerce
from ecommercetools import seo
import csv
import pandas as pd
searching = input('What do you want to search?')
results = seo.get_serps(searching)
df = pd.DataFrame(results.head(20)) # Convert result into data frame.
df.to_csv("ScanOutput.csv",mode="a")
Thank you
I tried with several module but stuck somehow. Any help would be appreciated
If this is the content of your .csv file called file.csv:
a,b,c
1,2,3
k,l,m
then you can read it and loop row by row like so:
import csv
# read file.csv and print each row
with open('file.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
This answer doesn't use pandas (but csv which is simpler) which I think is ok until you don't have gigabytes of data or the data is very complex
I have a csv file of stock market tickers name which I want to import into python as a list in order to use it in web.DataReader(ticker_list, 'yahoo', start, end). Since my csv file is only a single column, is there a efficient way to do this? My list is as simple as below image
Would this work for you?
import csv
with open('1.csv') as f:
reader = csv.reader(f)
data = list(reader)
Afterwards you could access the lists contents by using something like
print(data[0]) for an output of ['^GSPC'].
I hope this helps
I am using pandas to load, modify and save csv files. Actually pandas and its dataframe functionality is only a workaround for me, as I do not need this. I only need it, because I have to modify my csv file.
I need to remove certain lines (rows). My current code is as follows:
import pandas as pd
test=pd.read_csv('myfile.csv', sep=';', skiprows=[0,1,3,4,6])
test.to_csv('myoutputfile.csv', index=False, sep=';')
I would like to maniuplate the csv file directly. I know that with import csv and next(reader) I could for example skip the first row. However, I need to skip these specific rows: skiprows=[0,1,3,4,6] and I don't know how to do this. So is there a way to modify the csv files without using pandas and save the changes?
generally, if you have a list of rows to skip, you can use something like this:
import csv
skiprows = (0,1,3,4,6)
with open('myfile.csv') as fp_in:
reader = csv.reader(fp_in, delimiter=';')
rows = [row for i, row in enumerate(reader) if i not in skiprows]
with open('myoutputfile.csv', 'w', newline='') as fp_out:
writer = csv.writer(fp_out)
writer.writerows(rows)
I made csv file in my python code itself and going to append next data in ti it but the error is comming
io.UnsupportedOperation: not readable
I tried code is:
df.to_csv('timepass.csv', index=False)
with open(r'timepass.csv', 'a') as f:
writer = csv.reader(f)
your_list = list(writer)
print(your_list)
want to append next data and store in the same csv file. so that csv file having both previous and current data.
so please help me to find out..
Thanks in advance...
It is so simple just try this:
import pandas as pd
df = pd.read_excel("NSTT.xlsx","Sheet1") #reading Excel
print(df) #Printing data frame
df.to_excel("new.xlsx") #Writing Dataframe into New Excel file
Now here if you want to append data in the same file then use
df.to_excel("new.xlsx","a")
And no need to add in a list as you can directly access the data same as a list with data frame only you have to define the location .
Please check this.
You can use pandas in python to read csv and write csv:
import pandas as pd
df = pd.read_csv("csv file")
print(df)
Try:
with open(r'timepass.csv', 'r') as f:
reader = list(csv.reader(f))
print(reader)
Here you are opening your file as r, which means read-only and assigning the list contents to reader with list(csv.reader(f)). Your earlier code a opens the file for appending only where in the documentation is described as:
'a' opens the file for appending; any data written to the file is
automatically added to the end
and does not support the read().
And if you want to append data to the csv file from a different list, use the with open as a with the writer method.
with open('lake.csv','a') as f:
csv.writer(f,[1,2,3]) #dummy list [1,2,3]
Or directly from the pandas.DataFrame.to_csv method from your new dataframe, with header = False so as not to append headers:
df.to_csv('timepass.csv', index=False)
df_new.to_csv(r'timepass.csv', mode='a', header=False) #once you have updated your dataframe, you can directly append it to the same csv file
you can use pandas for appending two csv quickly
import pandas as pd
dataframe1=pd.read_csv("a.csv")
dataframe2=pd.read_csv("b.csv")
dataframe1=dataframe1.append(dataframe2)
dataframe1=dataframe1.reset_index(drop=True)
dataframe1.to_csv("a.csv")