Aligning a list by a changing value - python

I have my script doing the following for the headers of the text file:
file.write("{:50} {:50} {:50}\n".format("Name", "Count", "Price"))
Then a function is called through multiple threads that write out a list:
file.write("{:50}{:50}{:50}".format(*map(str, input)).strip()+ "\n")
This gets the formatting close to the header but they all seem to be off by a bit, i'm assuming it has to do with the variable length of the contents of the list. But I'm not sure how to change the 50's in the list to make it align correctly.
My output looks like this currently:
Any help would be appreciated.
EDIT
Problem has been fixed.

The variance is caused by your inputs containing extra whitespace after, pushing the line lengths past the fifty characters.
You need to strip the inputs, not the str.format() output:
file.write("{:50}{:50}{:50}\n".format(*(str(i).strip() for i in input)))
Next, open your file in a fixed-width font. Your text is not aligning because different letters take up different widths; m is wider than i.

Related

How to parse and preserve text formatting (Python-Docx)?

I'm using Python-Docx to export all the data from a 500-page Docx file into a spreadsheet using pandas. So far so good except that the process is removing all character styles. I have written the following to preserve superscript, but I can't seem to get it working.
for para in document.paragraphs:
content = para.text
for run in para.runs:
if run.font.superscript:
r.font.superscript = True
r = para.add_run(run.text)
scripture += r.text
My Input text might me, for example:
Genesis 1:1 1 In the beginning God created the heavens and the earth.
But my output into the Xlsx file is:
Genesis 1:1 1 In the beginning God created the heavens and the earth. (Still losing the superscript formatting).
How do I preserve the font.style of each run for export? Perhaps more specifically, how do I get the text formatting from each run to be encoded into the "scripture" string?
Any help is greatly appreciated!
You cannot encode font information in a str object. A str object is a sequence of characters and that's that. It cannot indicate "make these five characters bold and the following three characters italic. There's just no place to put that sort of thing and the str data type is not made for that job.
Font (character-formatting) information must be stored in a container object of some sort. In Word, that's a run. It HTML it can be a <span> element. If you want character-formatting in your spreadsheet, you'll need to know how character formatting is stored in the target format (Excel maybe) and then apply it to text in that export format on a run-by-run basis.
There are some other problems with your code you should be aware of:
the r in r.font.superscript = True is being used before being defined. The r = para.add_run(run.text) line would need to appear prior to that line to avoid problems. I wouldn't bother here because it's not actually doing anything here it turns out, but names need to be defined before use.
You are doubling the size of the source paragraph by adding runs to it. This part actually contributes nothing because you then call run.text which as we mentioned cannot contain any character-formatting information and so it gets stripped back out.
The same result as your current code can be achieved by this:
scripture = "".join(p.text for p in document.paragraphs)
but I think you'll at approach like:
Parse out bits that go in separate cells
Within the text that goes into a single cell, write a "rich-text" cell something like that described here for XlsxWriter: https://xlsxwriter.readthedocs.io/example_rich_strings.html

cleaning the format of the printed data in python

I am trying to compare two lists in python and produce two arrays that contain matching rows and non-matching rows, but the program prints the data in an ugly format. How can I clean I go about cleaning it up?
If you want to read the file without the \n character, you might consider doing the following
lines = list1.readlines()
lines2 = list2.readlines()
would read your file without the "\n" characters
Alternatively, for each line, you can do .strip("\n")
The "ugly format" might be because you are using print(match) (which is actually translated by Python to print ( repr(match) ), printing something that is more useful for debugging or as input back to Python - but not 'nice'.
If you want it printed 'nicely', you'd have to decide what format that would be and write the code for it. In the simplest case, you might do:
for i in match:
print(i)
(note your original list contains \n characters, that's what enumerating an open text file does. They will get printed, as well (together with the `\n' added by print() itself). I don't know if you want them removed or not. See the other answer for possible ways of getting rid of them.

Pymel: How do I extract these vector floats from a complex .TXT file?

I am having trouble wrapping my head around how I would extract the float values from a complex text file in Pymel. I am not a programmer, I am an artist, however I am in need of creating a script for a specific workflow process and I have a beginner level knowledge of python.
My goal: to create objects in 3D space with (x,y,z) coordinates parsed from a specific text file from another program.
Ex. of text file:
point 1 8.740349 -4.640922 103.950059
point 2 8.520906 3.447561 116.580496
point 3 4.235010 -7.562914 99.632423
etc., etc
there's much more space in my text file between the point #'s and the vector floats.
I want to create a dictionary that I will use to create my objects in my 3D program.
For example,
myDictionary = {(point 1),[8.740349,-4.640922,103.950059]), etc. }.
This is my code snippet so far:
def createLocators():
global filePath
global markerNum
global markerCoord
print "getting farther! :)"
with open(filePath,'r') as readfile:
for line in readfile:
if "point" in line:
Types = line.split(' ')
markerNum = [Type[1] for Type in Types]
markerCoord = [Type[2] for Type in Types]
print markerNum, markerCoord
As you can see in the code, the space between the information is long. I figure if I can remove that space I can get two data sets that will be easier to work with. There is also many more lines in the text document that I don't care about, hence the if statement to filter only lines that start with "point". When I run createLocator() to test to see if it's splitting up the lines into my two lists it runs fine, but the print looks empty to me.
ex.
[' '] [' ']
I've tried googling and searching answers here on SO, and searching both Pymel and regular python documentation for what I'm doing wrong or better approaches, but I have to admit the extent of my knowledge ends here.
What am I doing wrong? Is there a better and more efficient way to extract the data I need that I'm missing?
Thanks for reading!
First, you probably do not want to be splitting on that massive string of spaces. In fact what you almost certainly want is to just use line.split() with no arguments, as this will split apart all text on any kind, and any amount, of whitespace. i.e:
>>> 'A B C\t\n\t D'.split()
['A', 'B', 'C', 'D']
Then, assuming the format you've shown is correct, you should need need to get Types[2:5], i.e. the second, third, and fourth elements of Types, for the coordinates.
Beyond this, you should not be using capital names for local variables, and you should not be using global variables. Use function arguments instead, and rename Types to split_line or something.

I want to read in strings to the new line character in Python 2.7

I have a long text file that I am trying to pull certain strings out of. The length of these strings are variable with the text file but are always located after certain identifiers. So for example say my text file looks like this:
junk text...
Name:
Age:
Robert
twenty
four.
junk text...
I always know that the "Robert" string is located at "Age:\n\n" but I am not sure how long it is only that it will end at a "\n\n" and the same principle with the "twenty four." string. I have tried using
namepos1 = string.find("Age:")
namepos2 = namepos1 + 6
this will give the starting location of the string I want but I do not know how to save it into a variable such that it always saves the whole string up to the two new line characters. If it was a set length and not variable I think I could use:
name = string[namepos2:length]
but any help would be greatly appreciated. I may have to go about doing it completely different, but this is the first way I have thought about it and tried to do it.
Thanks!
You could do this by finding age, then moving forward your cursor two lines if you would like to do that, if you want the entire section of text after the "junk", and you know how long that text is, this would also work:
lookup = 'age'
lines=[]
with open('C:/Users/Luke/Desktop/Summer 2016/Programs/untitled5.txt') as myFile:
for num, line in enumerate(myFile, 1):
if lookup in line:
lines.append(num+2)
ofile=open('C:/Users/Luke/Desktop/Summer 2016/Programs/untitled5.txt')
line=ofile.readlines()
interestinglines=''
for i in range(len(lines)):
interestinglines+=(line[lines[i]]+'\n')
you may need to tinker with it a bit, but I believe this should reproduce mostly what you're looking for. The '\n' is added onto the line[lines[i]] so that you may save it to a new file.
After you found the location in string, you can split the String by \n\n and get the first item.
s = file_str[namepos2 :]
name = s.split('\n\n')[0]

Importing a text file to create a list in Python 3.x?

I can't seem to figure out how to use values given in a text file and import them into python to create a list. What I'm trying to accomplish here is to create a gameboard and then put numbers on it as a sample set. I have to use Quickdraw to accomplish this - I kind of know how to get the numbers on Quickdraw but I cannot seem to import the numbers from the text file. Previous assignments involved getting the user to input values or using an I/O redirection, this is a little different. Could anyone assist me on this?
Depends on the contents of the file you want to read and output in the list you want to get.
# assuming you have values each on separate line
values = []
for line in open('path-to-the-file'):
values.append(line)
# might want to implement stripping newlines and such in here
# by using line.strip() or .rstrip()
# or perhaps more than one value in a line, with some separator
values = []
for line in open('path-to-the-file'):
# e.g. ':' as a separator
separator = ':'
line = line.split(separator)
for value in line:
values.append(value)
# or all in one line with separators
values = open('path-to-the-file').read().split(separator)
# might want to use .strip() on this one too, before split method
It could be more accurate if we knew the input and output requirements.
Two steps here:
open the file
read the lines
This page might help you: http://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects

Categories

Resources