我正在学习python(2.7).我了解到我们可以使用以下方法将字符串和变量放在一起打印:
x = "Hello"
y = "World"
Run Code Online (Sandbox Code Playgroud)
使用逗号:
print "I am printing" , x, y # I know that using comma gives automatic space
Run Code Online (Sandbox Code Playgroud)
通过使用串联:
print "I am printing" + " " + x + " " + y
Run Code Online (Sandbox Code Playgroud)
通过使用字符串格式化器
print "I am printing %s %s" % (x, y)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,所有三个打印相同:
I am printing Hello World
Run Code Online (Sandbox Code Playgroud)
这三者之间有什么区别,是否存在一个优先于另一个的特定情况?
我正在使用 seaborn 创建小提琴图。现在我正在创建不成比例的小提琴图(所以所有值都在 0 和 1 之间),但生成的小提琴图很不合适。它的底部范围为负值,其顶部范围为大于 1 的值。下面是我运行以测试它的示例:
import seaborn as sns
import numpy as np
y = np.asarray([.1725,.1825,.163,.1625,.93,.943,.893,.93,.11225,.93,.812,.832,.9425,.953,.8525,.993,.963,.1425,.113,.752])
x = np.asarray([1]*len(data))
sns.violinplot(x=x,y=y)
sns.plt.show()
Run Code Online (Sandbox Code Playgroud)
显然没有一个值超出范围 [0,1],但小提琴图看起来很奇怪:
帮助将不胜感激!
我正在使用带有docx的python 2.7,我想根据条件更改表格中单元格的背景和文本颜色.
我找不到任何关于单格格式化的有用资源
有什么建议?
编辑1
我的代码
style_footer = "DarkList"
style_red = "ColorfulList"
style_yellow = "LightShading"
style_green = "MediumShading2-Accent6"
style_transperent = "TableNormal"
for a,rec in enumerate(data):
#V headinh se piše prvo polje iz table heada
document.add_heading(rec['tableHead'][0][0], level=1)
image_path = imageFolder + "\\" + slike[a]
document.add_picture(image_path, height=Inches(3.5))
#y += 28
#worksheet.insert_image( y, 1,imageFolder + "/" + slike[a])
for i, head in enumerate(rec['tableHead']):
table = document.add_table(rows=1, cols = len(head))
hdr_cells = table.rows[0].cells
for a in range(0,len(head)):
hdr_cells[a].text = head[a]
for a,body in enumerate(rec['tableData']): …Run Code Online (Sandbox Code Playgroud) 下面的代码片段基本上创建了一个表格,其中包含新 Word 文档中所需的行数和列数,即 2 列和 14 行。然后相应地将内容添加到行和列。
from docx import Document
newDoc=Document()
newDoc.add_heading ('GIS Request Form')
newDoc.add_paragraph()
#inserting a table and the header and value objects to the table
table=newDoc.add_table(rows=14,cols=2)
table.style='Table Grid'
table.autofit=False
table.columns[0].width=2500000
table.columns[1].width=3500000
#inserting contents into table cells
for i in range(0,14):
row=table.rows[i]
row.cells[0].text=reqdheaderList[i]
row.cells[1].text=reqdvalueList[i]
Run Code Online (Sandbox Code Playgroud)
我一直试图将第 1 列中所有内容的内容加粗,但它不起作用。
#inserting contents into table cells
for i in range(0,14):
row=table.rows[i]
row.cells[0].text=reqdheaderList[i]
row.cells[0].paragraphs[0].add_run(line[0]).bold=True
row.cells[1].text=reqdvalueList[i]
Run Code Online (Sandbox Code Playgroud)
帮助?
这些天我正在学习SQLAlchemy.当我想从json加载一个对象并将其保存到MySQL时,事情变得困难,因为我的模型中的字段超过20,我想知道是否有更好的方法来做到这一点.
我的原始代码如下:
class User(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
name = Column(String)
json_str = """{"id": 1, "name": "yetship"}"""
obj = json.loads(json_str)
user = User(id=obj.get("id"), name=obj.get("name"))
Run Code Online (Sandbox Code Playgroud)
它可以工作,但随着我添加更多字段,它变得非常糟糕.
我已经获得了3d高斯分布的均值和sigma,然后我想用python代码绘制3d分布,并获得分布图。
在Python 3中,我制作程序以在Twitter中提取帖子和喜欢:
import tweepy
import pandas as pd
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
Run Code Online (Sandbox Code Playgroud)
此函数接收配置文件的教学分类(仅适用于数据库组织)和配置文件的名称.它创建一个包含字典的列表,然后返回:
def linhadotempo(posicao, valor):
tela = api.user_timeline(valor)
bolha = []
for status in tela:
dicionario = {"nome": valor, "posicionamento": posicao, "posts_links": status.text, "curtidas": status.favorite_count}
bolha.append(dicionario)
return bolha
Run Code Online (Sandbox Code Playgroud)
Twitter个人资料的名称列表及其教学评级.然后转换成数据帧:
data = {
'nome': ['jeanwyllys_real', 'lucianagenro', 'jairbolsonaro', 'MBLivre'],
'posicionamento': ['esquerda', 'esquerda', 'direita', 'direita']
}
perfis = pd.DataFrame(data, columns=['nome','posicionamento'])
perfis.reset_index()
index nome posicionamento
0 …Run Code Online (Sandbox Code Playgroud) 我可以使用段落对象在表格单元格中选择字体大小,颜色,粗体等.但是,add_paragraph()似乎总是将一个前导\n插入到单元格中,这会混淆某些表格上的格式.
如果我只是使用该cell.text('')方法,它不会插入此换行符,但我无法控制文本属性.
有没有办法消除这个领先的换行线?
这是我的功能:
def add_table_cell(table, row, col, text, fontSize=8, r=0, g=0, b=0, width=-1):
cell = table.cell(row,col)
if (width!=-1):
cell.width = Inches(width)
para = cell.add_paragraph(style=None)
para.alignment = WD_ALIGN_PARAGRAPH.LEFT
run = para.add_run(text)
run.bold = False
run.font.size = Pt(fontSize)
run.font.color.type == MSO_COLOR_TYPE.RGB
run.font.color.rgb = RGBColor(r, g, b)
Run Code Online (Sandbox Code Playgroud) 我想使用 python 在 docx 文件中写入/包含两个以上的表。如何使用 python 将此结构写入 docx 文件中的表?
我为单个表尝试了以下代码。现在我想在一个 docx 中创建 2 个表。
table = document.add_table(rows=rows_no,cols=1)
Timeline = row[5]
print (row[0],row[3],"Timing:",row[5])
cells = table.add_row().cells
cells[0].paragraphs[0].add_run( Compliance_requirements).bold = True
cells[0].paragraphs[1].add_run( "Obs: "+Finding_Description).text = True
cells[0].paragraphs[2].add_run( "requitements: "+requirements).text = True
cells[0].paragraphs[3].add_run( "Timeline: Need"+Timeline+" days of notice period .").text = True
document.add_paragraph()
Run Code Online (Sandbox Code Playgroud) 我是python的初学者.但是,当我尝试使用readline()方法时,我遇到了一些问题.
f=raw_input("filename> ")
a=open(f)
print a.read()
print a.readline()
print a.readline()
print a.readline()
Run Code Online (Sandbox Code Playgroud)
和我的txt文件是
aaaaaaaaa
bbbbbbbbb
ccccccccc
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在Mac终端上运行它时,我得到了这个:
aaaaaaaaa
bbbbbbbbb
ccccccccc
Run Code Online (Sandbox Code Playgroud)
似乎readline()根本不起作用.但是当我禁用print a.read()时,readline()会恢复工作.
这让我很困惑.有什么解决方案可以同时使用read()和readline()吗?
python ×10
python-docx ×4
docx ×3
cell ×1
json ×1
list ×1
matplotlib ×1
plot ×1
python-3.x ×1
r ×1
seaborn ×1
sqlalchemy ×1
string ×1
tweepy ×1
twitter ×1
violin-plot ×1