Python prints tabular data, leaving the correct spaces and paragraph asterisks or notes

Source: Internet
Author: User
Tags python script

Python prints tabular data, leaves the correct spaces, and formats the typed

The code is as follows:

def printpicnic (itemsdict,leftwidth,rightwidth):
Print (' PICNIC ITEMS '. Center (leftwidth + rightwidth, '-'))
For k,v in Itemsdict.items ():
Print (K.ljust (leftwidth, '. ') +str (v). Rjust (Rightwidth))

Picnicitems = {' Sandwitches ': 4, ' apple ': +, ' cups ': 4, ' Cookies ': 8000}
Printpicnic (picnicitems,12,5)
Printpicnic (picnicitems,20,6)
The effect is as follows:

Paragraph asterisk or comment, when you edit a Wikipedia article, you can create an unordered list, each of which occupies one row and an asterisk in front of it. This can be added manually, but when you have a very large list, you want to add asterisks to the front.

You can automate this task with a small Python script

So the following things need to be done:

1. Pasting text from the Clipboard

2. Do some processing on it

3. Copy the new text to the Clipboard

So the prototype is like this:

Import Pyperclip

Text = Pyperclip.paste ()

#TODO: Seperate lines and add stars

Pyperclip.copy (text)

So the part that needs to be done is to split the string into a list and then add an asterisk to each element of the list and then join the string again.

The complete code is as follows:

Import Pyperclip
Text = Pyperclip.paste ()
lines = Text.split (' \ n ')
For I in range (len (lines)):
Lines[i] = ' # ' + lines[i]
Text = ' \ n '. Join (lines)
Pyperclip.copy (text)

Python prints tabular data, leaving the correct spaces and paragraph asterisks or notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.