read an excel file in python by importing csv [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can I read an excel file in python by importing csv? How can I read the columns and the rows? For instance I want to write a piece of code, which classifies a certain column if its value is greater than a determined number as accepted and otherwise (if less than that number) not accepted.
I do not want to read the file in reverse order.

Use the csv package. Check out the documentation here:
https://docs.python.org/2/library/csv.html.
Specifically for your issue, I'd do something like this:
import csv
with open(myfile) as fp:
rows = list(csv.reader(fp))
for row in rows:
# let's grab all columns beyond the fifth
for column in row[5:]:
# do stuff
Personally, I like to label my columns. I'd recommend exploring csv.DictReader so that you can access your data by label, instead relying on arbitrary column headers, which are kind of arbitrary.
Just make sure to export your Excel file as a csv.

Related

Sort and order output data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I have developed a small program that reads and outputs the live data of a machine. However, the data is outputted in a confusing and unordered way.
My question is, what exactly can I do to sort the output data e.g. in a table.
Best
enter image description here
You wrote many (topic, payload) tuples to a file, test_ViperData.txt.
Good.
Now, to view them in an ordered manner, just call /usr/bin/sort:
$ sort test_ViperData.txt
If you wish to do this entirely within python,
without e.g. creating a subprocess,
you might want to build up a long list of result tuples.
results = []
...
results.append((topic, payload))
...
print(sorted(results))
The blank-delimited file format you are using is OK, as far as it goes.
But you might prefer to use comma-delimited CSV format.
Then you could view the file within spreadsheet software,
or could manipulate it with the standard csv module
or various pandas tools.
When you review the text file next week,
you might find it more useful if
each record includes a timestamp:
import datetime as dt
...
results.append((topic, payload, dt.datetime.now()))

Is is there any reason for keeping the extension `csv` in the method name `read_csv` in Pandas? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Initially, I used to think that read pandas.read_csv in Pandas used to read only the files end with .csv.
Later I realized that this method is used to read text file also.
Is there any reason for keeping the extension csv in the method name if the method does not process only csv files?
the function name reflects the format of the data to read, not the name of the file it is read from, and is hence correctly named. .read_csv(), just as its siblings .read_xml() and .read_hdf() etc., can read from any file-like object, which is not necessarily backed by an actual file on disk.

How can I make an excel file and wirte to it using python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to program a script in python that writes an excel file with three categories and it's input being three text files.
It would be much appreciated if you could help me out because I have literally no idea how to work with excel files.
Thanks in advance!
A reasonable path to follow when processing Excel with Python is Pandas' dataframe. You can get started with reading some documentation from them and get experimenting with it.,
pip install pandas
import pandas as pd
df = pd.read_excel("path_to_file.xls", sheet_name="Sheet1")
df.to_excel(filepath, sheet_name='Sheet1')
Starting from here you can do a lot with pandas and excel
I see people recommending xlwt. XLWT keeps formats for excel and does styling which is another reasonable path, but in comparason lacks some functionaility when processing datas. In either cases, both only supports legacy (pre-2007 for Pandas and pre-2003 for XLWT) verisons of Excel

What is the best way to write and store data in python 3 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to store data from stock market transactions I make. I want to have a file like an excel workbook, with every buy or sell price being neatly listed. I have looked at some options, including pickle, xlwt, and pandas. However, I cannot find any that can write onto a pre existing file
The information I will be storing will look like this:
DATE, TIME, STOCK_INDICATOR, BUY/SELL, PRICE
I will need the program to be able to write new rows every time a purchase is made.
Pandas has a method to write data to a csv files, if this is what you mean (pandas.DataFrame.to_csv). You can specify 'write' mode, to rewrite all data, or 'append' mode, if you want to update the file with new rows.
I guess there is no best way.
I would probably do it in a list-dictonary like fashion, although not sure if this would be the greates way.
something like:
myPurchases = [
{"date": []},
{"Time": []},...
]
// and if a new purchase is made I just would write a function which gets the list
// and appends a new entry into the dictonarys.

How to write a paragraph to an excel file, each word in new cell? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a corpus of around "1 million Urdu sentences" in a .txt file (notepad). I want to save each "Urdu word" in a separate cell of an excel sheet.
Can some one help me with any regex or code fragment tested? I am using python pycharm.
I don't know if it can be done through some easy way or trick or using some other tool?
My first thought is to import the file using Excel directly. Data --> From Text, and then separate the words using " " or whatever delimiter is used between words

Categories

Resources