I'm looking to create a new .csv file using python and then write to the file after that. I couldn't find a command to create a new file using the CSV library. I thought something like
NewFile = csv.creatfile(PATH)
might exist but I couldn't find something like that.
Thank you in advance! Happy to answer any questions you may have!
The csv module doesn't handle file creation; Python handles file creation as a builtin function called open -- once you have a file handle created using open, you can use it with csv.writer or csv.DictWriterto write data to the file.
Related
So for a previous question I asked how to add a custom made xmlMap to an excel file in python and I was "successful" by opening the xlsx file as an archive and extracting the file structure, followed by adding the xmlMaps.xml file to the structure and including it in the "rels". I can now open the excel file and see that the xml source map is attached, but I cannot export it. It mentions that I need to set the attribute "xmlmap.isExportable" to True, but I have no clue about how to do this, preferably using python.
All I have found on google is this: https://learn.microsoft.com/en-us/office/vba/api/excel.xmlmap.isexportable
My old question regarding the case: Adding XML Source to xlsx file in python
Any help is greatly appreciated
Best regards
Martin
Turns out I just needed to understand how a xlsx file works and how xml connections are stored and referenced throughout multiple sub files
I developed a software which can be automatically updated, so I need external-placed config file/files. For now I use json file to store user-input variables like user name etc. But I am not sure how the program itself should be controlled. I mean things like checking if program is opened for first time after update to know if update notes should be shown, what functions were already used etc. For now I am doing it with things like:
if os.path.exists(control_file_1):
actions_1
if os.path.exists(control_file_2):
some other actions unrelated to actions_1
it is independent from the files content - so there is no need to read the file content - which is convenient.
What functions should be used to store those information in one file and read them efficiently? Just normal file.read() etc? It seems not very clean-code friendly.
Thanks
UPDATE:
Looks like a ConfigParser is a way to go. Am I right? Or are they any better ways to accomplish what I am going for?
Given that you need to have config information stored in a file. If you choose to have that information in a file that contains a json record then it is the most convenient if the file is used internally and updating and reading the record in the file is easy (treat it as a dict)
However, if you want a more universal config.ini reader then you can go with ConfigParser class which you can use directly or create your own wrapper
class MYConfig_Parser(ConfigParser):
so that you can check stuff in the constructor like if mandatory entries are available etc before processing the entries.
I would like to produce some custom output with python with data from Tableau files. I dont have access to the Tableau server to run the 'Tabpy' library.
Is there any other way to do it?
Thank you in advance
You may find the following link useful.
https://community.tableau.com/thread/152463
One of the posts in the thread mentioned the following which is worth exploring:
If you're looking to generate a TWBX dynamically, you should rename
your .twbx file to .zip, extract the contents and you can do whatever
you want with those in Python to dynamically create or adjust a
workbook file. The structure / definition of the workbook file is just
XML so no special code needed to read and parse that.
Is it possible to have a script (written in Python) that can change a content of a certain text file according to a database records.
For example: I have a DB that has some records of different types and a script that retrieves a single record from the database and according to that record it automatically changes a content of the text file that is somewhere on the system (e.g. tempo_file.txt).
I know that Python can read from database, read from file and input to the file. But what kind of logic should I apply to overcome this problem? Thanks.
You have to realize, when read from I file, you actually read it to the memory. You can manipulate it in your program.
Then you can flush the bytes in your memory into the disk. AKA save it or write it. If you want to "automatically change something", I guess you have to just read from the file and write to the file that has the same name.
There really is no way to manipulate it on the hard disk.
what kind of logic should I apply to overcome this problem?
Well quite simply:
read from the database
open the text file
write to the text file
close the text file
lather, rinse, repeat
Is there is a way to rename the file name inside the compressed file ,but without extract it, in python ??
No. The python ZipFile module does not provide any way to do that.
You can extract the archive into memory, and then rewrite it with the new filename for the file in question, but that is memory intensive and not what you want to do.
You may be able to edit the zipfile's header and information fields, but the standard Python interface doesn't have an easy way to do so.