How to open and edit a CSV file with 2 different programs? - python

I am building a bot with python, and this bot saves data into CSV file, so every time the python open the file, edit, save.
The thing is - I am connected to that CSV using VBA (through SQL), and every X seconds the VBA reads the CSV and calculates some things from that CSV file.
The problem comes when there is this moment where the python is writing to that CSV and the VBA wants to read from that CSV on the same time or vice versa, if the VBA reads from the csv and then python is crashing due to permission error.
What are the solution for those kind of things? that more than 1 program needs to access on the same time the CSV file?

Related

Writing and Reading from CSV - Python

I am working on an IoT project, where a sensor sends data and is stored in a CSV file. A python program then reads this file every 30 seconds and reads 300 rows and do some processing. So, at 30 second, rows 1-300 will be read and at 60 second, rows 300-600 will be read.
At the same time data is getting stored into this as well.
So is it possible in some way to implement this in python where one program is writing data into a file while another is reading from the file.

How to automatically run the python script when a new csv file appears

I need a solution so that a ready python script will run every time a new csv file appears in the folder. The script uses this csv file, which will always have the same name, but it will be updated from time to time and then the script should run automatically.
I am not an advanced python user, so I can't handle it myself and the solution will make my work much easier. I am asking for tips on how to do it or send some tutorial or code.
Have a nice day everyone :)

How can I open (pop up) an excel file for user using openpyxl in python?

I am using the openpyxl library to write data into an excel file, I want this file to be automatically opened (to pop up) for user when its finished writing data, I've looked so much but found nothing more than saving the file, which I have already done.
I think one possibility to do this would be with os.startfile:
import os
os.startfile('your_excel_file.xlsx')

Can I use the subprocess method to pass data back and forth from Python and R without writing to a file?

The subprocess method in python seems ideal for running an R script and also keeping R code separate from Python code. It's pretty easy to call up an R script that does some statistics on something like a CSV file. But how do I get that R script to work on data that python has stored in memory?
I have only been able to find examples where information is written and then read from CSV files.
For example: Use python to write the string of numbers to a CSV file, then call an R script which reads that CSV file and subsequently writes to another CSV file, and finally python reads the CSV file written by R.

python beginner questions

i just installed python
i am trying to run this script:
import csv
reader = csv.reader(open("some.csv", "rb"))
for row in reader:
print row
i am running on windows.
do i have to type each line individually into python shell or can i save this code into a text file and then run it from the shell?
where does some.csv have to be in order to run it? in the same c:\python26 folder?
what is this code supposed to do?
Yes, you can create a file. The interactive shell is only for learning syntax, etc., and toying with ideas. It's not for writing programs.
a. Note that the script must have a .py extension, e.g., csvprint.py. To run it, you enter python csvprint.py. This will try to load csvprint.py from the current directory and run it.
The some.csv file has to be in the current working directory, which doesn't have to be (in fact, almost never should be) in the Python folder. Usually this will be you home directory, or some kind of working area that you setup, like C:\work. It's entirely up to you, though.
Without knowing the csv module that well myself, I'm guessing it reads CSV separated values from the file as tuples and prints each one out on the console.
One final note: The usual way to write such logic is to take the input from the command-line rather than hard-coding it. Like so:
import csv
reader = csv.reader(open(sys.argv[1], "rb"))
for row in reader:
print row
And run it like so:
python csvprint.py some.csv
In this case you can put some.csv anywhere:
python csvprint.py C:\stuff\csvfiles\some.csv
When you have IDLE open, click File > New Window. (Or hit Ctrl + N)
This opens up a new window for you that's basically just a text editor with Python syntax highlighting. This is where you can write a program and save it. To execute it quickly, hit F5.
You can do both! To run the code from a text file (such as 'csvread.py', but the extension doesn't matter), type: python csvread.py at the command prompt. Make sure your PATH is set to include the Python installation directory.
"some.csv" needs to be in the current directory.
This code opens a Python file descriptor specifically designed to read CSVs. The reader file descriptor then prints out each row of the CSV in order. Check the documentation out for a more detailed example: http://docs.python.org/library/csv.html
Type the code into a *.py file, and then execute it.
I think the file should be in the same folder as your *.py script.
This opens a file stored in comma separated value format and prints the contents of each row.
All import does "Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way". The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. There is no “CSV standard”, so the format is operationally defined by the many applications which read and write it. The lack of a standard means that subtle differences often exist in the data produced and consumed by different applications. These differences can make it annoying to process CSV files from multiple sources. Still, while the delimiters and quoting characters vary, the overall format is similar enough that it is possible to write a single module which can efficiently manipulate such data, hiding the details of reading and writing the data from the programmer.
The CSV module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel. Programmers can also describe the CSV formats understood by other applications or define their own special-purpose CSV formats. All your code is doing is looping through that file.

Categories

Resources