如何实现换行显示def getActors(filename): actors = dict() # 打开xlsx文件,并获取第一个worksheet wb = openpyxl.load_workbook((filename)) ws = wb.worksheets[0] # 遍历Excel文件中的所有文件中的所有行 for index, row in enumerate(ws.rows): # 跳过第一行的表头 if index == 0: continue # 获取电影名称和演员列表 fileName, actor = row[0].value, row[1].value.split('/') # 遍历该电影所有演员,统计参演电影 for a in actor: actors[a] = actors.get(a, set()) actors[a].add(fileName) return actors data = getActors('实践2素材.xlsx') print(data)
时间: 2024-02-14 08:17:15 浏览: 75
您可以在每个 print 语句后面添加换行符 "\n" 来实现换行输出。例如,您可以这样写:
```
def getActors(filename):
actors = dict()
# 打开xlsx文件,并获取第一个worksheet
wb = openpyxl.load_workbook((filename))
ws = wb.worksheets[0]
# 遍历Excel文件中的所有文件中的所有行
for index, row in enumerate(ws.rows):
# 跳过第一行的表头
if index == 0:
continue
# 获取电影名称和演员列表
fileName, actor = row[0].value, row[1].value.split('/')
# 遍历该电影所有演员,统计参演电影
for a in actor:
actors[a] = actors.get(a, set())
actors[a].add(fileName)
return actors
data = getActors('实践2素材.xlsx')
print(data, "\n")
```
这样每次执行完 `print(data)` 后,会自动换行,使得下一行的输出与上一行的输出分开来,更加清晰易读。
相关问题
在cloudcompare的mainwindow.cpp中,用C++定义一个函数void MainWindow::DBH(),基于最小二乘法,通过获取当前选中的实体作为点云文件来源,实现对txt格式的树木点云文件的胸径计算
可以参考以下代码实现:
```cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include <QFileDialog>
#include <QMessageBox>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
typedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//连接信号与槽函数
connect(ui->actionOpen, SIGNAL(triggered(bool)), this, SLOT(openFile()));
connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(DBH()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Point Cloud"), "", tr("Point Cloud Files (*.pcd *.txt)"));
if(!fileName.isEmpty())
{
//清空界面
ui->qvtkWidget->update();
ui->textBrowser->clear();
//读取点云文件
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ>(fileName.toStdString(), *cloud) == -1)
{
//尝试读取txt格式文件
std::ifstream ifs(fileName.toStdString());
if (!ifs.is_open())
{
QMessageBox::critical(this, "Error", "Cannot open file!");
return;
}
std::string line;
while (std::getline(ifs, line))
{
pcl::PointXYZ p;
std::stringstream ss(line);
ss >> p.x >> p.y >> p.z;
cloud->push_back(p);
}
ifs.close();
}
//显示点云
pcl::visualization::PCLVisualizer vis("Point Cloud");
vis.setBackgroundColor(0.0, 0.0, 0.0);
vis.addPointCloud(cloud, "cloud");
vis.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");
ui->qvtkWidget->update();
//输出点云信息
std::stringstream ss;
ss << "Number of points: " << cloud->size() << std::endl;
ui->textBrowser->append(ss.str().c_str());
//保存点云
cloud_ = cloud;
}
}
void MainWindow::DBH()
{
if (!cloud_)
{
QMessageBox::warning(this, "Warning", "No point cloud selected!");
return;
}
//获取选中的点
std::vector<int> indices;
if (ui->qvtkWidget->getRenderWindow()->GetRenderers()->GetFirstRenderer()->GetViewProp("cloud"))
{
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor = ui->qvtkWidget->getRenderWindow()->GetRenderers()->GetFirstRenderer()->GetActors()->GetLastActor();
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper());
vtkSmartPointer<vtkDataSet> dataSet = vtkSmartPointer<vtkDataSet>::New();
dataSet = mapper->GetInput();
vtkSmartPointer<vtkUnsignedCharArray> pointColors = vtkSmartPointer<vtkUnsignedCharArray>::New();
pointColors = vtkUnsignedCharArray::SafeDownCast(dataSet->GetPointData()->GetScalars());
for (vtkIdType i = 0; i < pointColors->GetNumberOfTuples(); i++)
{
if (pointColors->GetValue(i) == 255)
{
indices.push_back(i);
}
}
}
if (indices.empty())
{
QMessageBox::warning(this, "Warning", "No point selected!");
return;
}
//计算胸径
double sum_x = 0.0, sum_y = 0.0, sum_z = 0.0, sum_xy = 0.0, sum_xz = 0.0, sum_xx = 0.0;
for (int i : indices)
{
sum_x += cloud_->points[i].x;
sum_y += cloud_->points[i].y;
sum_z += cloud_->points[i].z;
sum_xx += cloud_->points[i].x * cloud_->points[i].x;
sum_xy += cloud_->points[i].x * cloud_->points[i].y;
sum_xz += cloud_->points[i].x * cloud_->points[i].z;
}
double n = static_cast<double>(indices.size());
double a = (n * sum_xy - sum_x * sum_y) / (n * sum_xx - sum_x * sum_x);
double b = (sum_y - a * sum_x) / n;
double c = (n * sum_xz - sum_x * sum_z) / (n * sum_xx - sum_x * sum_x);
double d = (sum_z - c * sum_x) / n;
double D = 2.0 * sqrt(a * a + 1) * 1000.0; //单位:毫米
//输出胸径
std::stringstream ss;
ss << "DBH: " << D << " mm" << std::endl;
ui->textBrowser->append(ss.str().c_str());
}
```
该函数基于最小二乘法,通过获取当前选中的实体作为点云文件来源,实现对txt格式的树木点云文件的胸径计算。
创建一个影片类,属性参考豆瓣网站.https://movie.douban.com/subject/35242872/.将属性私有化提供公共方法来访问私有属性,同时创建这个类对象,给属性赋值,并输出这
创建一个电影类,我们可以参考豆瓣电影的信息结构。例如,可以考虑以下属性:电影ID、标题、导演、演员、评分、上映年份和简介。为了保护这些信息不直接暴露在外,我们将它们设置为私有变量,并提供公有的getter方法来获取这些数据。
```java
public class Movie {
// 私有属性
private int movieId;
private String title;
private String director;
private String[] actors;
private double rating;
private int year;
private String summary;
// 构造函数
public Movie(int movieId, String title, String director, String[] actors, double rating, int year, String summary) {
this.movieId = movieId;
this.title = title;
this.director = director;
this.actors = actors;
this.rating = rating;
this.year = year;
this.summary = summary;
}
// 公共方法获取私有属性
public int getMovieId() {
return movieId;
}
public String getTitle() {
return title;
}
public String getDirector() {
return director;
}
public String[] getActors() {
return actors;
}
public double getRating() {
return rating;
}
public int getYear() {
return year;
}
public String getSummary() {
return summary;
}
// 示例:创建并初始化电影对象
public static void main(String[] args) {
String[] actors = {"张三", "李四"};
Movie film = new Movie(35242872, "肖申克的救赎", "弗兰克·德拉邦特", actors, 9.6, 1994, "两个男人在监狱中的友情故事");
System.out.println("电影ID: " + film.getMovieId());
System.out.println("标题: " + film.getTitle());
System.out.println("导演: " + film.getDirector());
System.out.println("主演: " + Arrays.toString(film.getActors()));
System.out.println("评分: " + film.getRating());
System.out.println("上映年份: " + film.getYear());
System.out.println("简介: " + film.getSummary());
}
}
```
阅读全文
相关推荐















