How do I edit repository txt with python - python

So I am working on a python project for school and I wanted to save some variables to a raw txt. If there is a way to do it by only importing requests that would be great as the program I am using does not import git.
Thanks
-Amir Ahmed

First off, reading/writing from/to files is included in the Python Standard Library. Requests is a HTTPRequests library. The way to write to a text file is like so:
writer = open("path_or_relative_path.txt", "a")
writer.write("testdatatowrite\n")
writer.close()
This is the most basic way of writing to a text file.

Related

Storing RDF to Triple Store as input: Conversion from CSV to RDF

I am using Triple Store called Apache Jena Fuseki for storing the RDF as input But the thing is that i have data in CSV format. I researched a lot but didn't find direct way to convert CSV to RDF but there is tarql tool which is command line tool that can do the job but the thing is that i need a python script that directly converts my CSV to RDF form.
I have used the tools like openRefine and tarql but i need python script to do this job and i have read somewhere that owlready2 tool also used to convert CSV to RDF but when i used to visit the official site then i found that they are using OWL file for this work.
Thanks!
CSVW - CSV on the Web - is a W3C Recommendation for this. There is a python implementation.
Or you can run "tarql" from python by forking a subprocess.

Python - Opening a Password Protected CSV to read/write

I have been using python for only about two months so I am still quite new to coding.
Recently, in work, I wrote a code which opens an existing CSV file, performs a few operations and spits out a new CSV file. That bit I am happy with.
But what I want to know is what can I do in terms of securing the document and still running the code to open it? For example, I want to password protect this CSV file but want to prompt the user for the password which will be the only way to open/read the file.
Can anyone point me in the right direction please?
The CSV file is simply a formatted text (.txt) file. So to protect the file there are a few approaches.
Save CSV file then change permissions on it using OS commands - Password Protecting Excel file using Python
zip the csv file with password. Unfortunately, "The builtin zipfile module does not support writing password-encrypted files (only reading). Either you could use pyminizip. Refer to Create password protected zip file Python

extract text from .doc (not docx)

I checked mose question and answers in stackoverflow and others there is many way to open and read the .docx file not doc by using python
I already checked python-docx library but it only support to docx.
I wanna to open and extract text from .doc file(not docx). Plase help me Because I'm new in python
You can use Tika Python, it's an Apache Tika bindings for python. Another good library is a textract.
I created library to extract text from doc files. It works for C and Python
https://github.com/uvoteam/libdoc
usage example:
import extract_doc
with open('./test.doc', 'rb') as myfile:
data = bytearray(myfile.read())
print(extract_doc.extract_doc_text(data, len(data)))

Python - Reading a spreadsheet

What I need to know is, can I get Python to read a spreadsheet (preferably Microsoft Excel), then parse the information and input it into an equation?
It's for a horse-racing program, where the information for several horses will be in one excel spreadsheet, in different rows or columns. I need to know if I can run a calculation for each of those horses separately and then calculate a score for the given horse.
My suggestion is:
Save the Excel file as a csv comma separated value file, which is a plain text format and much easier to work with.
Use Python's built-in csv module to work with the data in csv format.
You can work with Excel files directly in Python (Excel 2003 format supported via the third party modules xlwt, xlrd) but this is much harder than working with CSV.
OpenPyXL ("A Python library to read/write Excel 2007 xlsx/xlsm files") has a very nice and Pythonic API.
Use xlrd package. It's on PyPI, so you can just easy_install xlrd
You can export the spreadsheet as a .csv and read it in as a text file, then process it. I have a niggling feeling there might even a CSV parsing python library.
AFAIK there isn't a .xls parser, although I might be wrong.
EDIT: I was wrong: http://www.python-excel.org/

Getting data from an Excel sheet

How do I load data from an Excel sheet into my Django application? I'm using database PosgreSQL as the database.
I want to do this programmatically. A client wants to load two different lists onto the website weekly and they don't want to do it in the admin section, they just want the lists loaded from an Excel sheet. Please help because I'm kind of new here.
Have a look at the xlrd package, which allows you to read Excel files in Python. Once you've read the data you can do whatever you want with it, including saving it to the database.
For a basic usage example, look at http://scienceoss.com/read-excel-files-from-python/
Use django-batchimport http://code.google.com/p/django-batchimport/ It provides a very simple way to upload data in Excel sheets to your Django models. I have used it in a couple of projects. It can be integrated very easily into your existing Django project.
Read the documentation on the project page to know how to use it.
It is built on XLRD.
Have a look at the presentation "Excel & Python" that Chris Withers gave at PyCon US:
"This lightning talk explains that you don't need to use COM or be on Windows to read and write native Excel files."
http://www.simplistix.co.uk/presentations/python_excel_09/excel-lightning.pdf
Programatically or manually? If manualy then just save the excel as a CSV (with csv or txt extension) and import into Postgresql using
copy the_data from '/path/to/csv/MYFILE.txt' DELIMITERS ',' CSV;
As I remember of this.
The best way is to save this sheet as plain text ( CSV or something )
And then load with some custom SQL script.
http://www.postgresql.org/docs/8.3/static/populate.html
Or have a look at SQLAlchemy if you're going to write some kind of script to help you with that.(http://www.sqlalchemy.org/)
If you want to use COM to interface excel (i.e. you are running on a Windows machine), see "Migrating Excel data to SQLite" - http://www.saltycrane.com/blog/2007/11/migrating-excel-to-sqlite-using-python/
I built django-batchimport on top of xlrd which is AMAZING. The only issues I had were with getting data into Django. Had nothing to do with any limitations of xlrd. It rocks. John's work is incredible.
Note that I've actually done some update work to django-batchimport and just released. Take a look: http://code.google.com/p/django-batchimport/
Just started using XLRD and it looks very easy and simple to use.
Beware that it does not support Excel 2007 yet, so keep in mind to save your excel at 2003 format.

Categories