The following is a snippet of code. The script takes the input files from a "Test" folder, runs a function and then outputs the files with the same name in the "Results" folder (i.e. "Example_Layer.shp"). How could I set it so that the output file would instead read "Example_Layer(A).shp"?
#Set paths
path_dir = home + "\Desktop\Test\\"
path_res = path_dir + "Results\\"
def run():
#Set definitions
input = path_res + "/" + "input.shp"
output = path_res + "/" + …Run Code Online (Sandbox Code Playgroud) 我正在阅读Python 3 的多处理主题并尝试将该方法合并到我的脚本中,但是我收到以下错误:
AttributeError:__ exit __
我使用带有i-7 8核处理器的Windows 7,我有一个大型shapefile,我想要处理(使用映射软件,QGIS),最好使用所有8个核心.以下是我的代码,我非常感谢您对此事的任何帮助:
from multiprocessing import Process, Pool
def f():
general.runalg("qgis:dissolve", Input, False, 'LAYER_ID', Output)
if __name__ == '__main__':
with Pool(processes=8) as pool:
result = pool.apply_async(f)
Run Code Online (Sandbox Code Playgroud) 我试图通过删除其基本名称中的最后四个字符来重命名子目录中存储的许多文件。我通常使用以下方法在一个目录glob.glob()中查找和重命名文件:
import glob, os
for file in glob.glob("C:/Users/username/Desktop/Original data/" + "*.*"):
pieces = list(os.path.splitext(file))
pieces[0] = pieces[0][:-4]
newFile = "".join(pieces)
os.rename(file,newFile)
Run Code Online (Sandbox Code Playgroud)
但现在我想在所有子目录中重复上述内容。我尝试使用os.walk():
import os
for subdir, dirs, files in os.walk("C:/Users/username/Desktop/Original data/"):
for file in files:
pieces = list(os.path.splitext(file))
pieces[0] = pieces[0][:-4]
newFile = "".join(pieces)
# print "Original filename: " + file, " || New filename: " + newFile
os.rename(file,newFile)
Run Code Online (Sandbox Code Playgroud)
该print语句正确打印我正在查找的原始文件名和新文件名,但os.rename(file,newFile)返回以下错误:
Traceback (most recent call last):
File "<input>", line 7, in <module> …Run Code Online (Sandbox Code Playgroud) 我正在尝试向 QTableWidget 中的每一行添加一个复选框,不幸的是它似乎只出现在第一行。这是我的代码:
data = ['first_row', 'second_row', 'third_row']
nb_row = len(data)
nb_col = 2
qTable = self.dockwidget.tableWidget
qTable.setRowCount(nb_row)
qTable.setColumnCount(nb_col)
chkBoxItem = QTableWidgetItem()
for row in range(nb_row):
for col in [0]:
item = QTableWidgetItem(str(data[row]))
qTable.setItem(row,col,item)
for col in [1]:
chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
chkBoxItem.setCheckState(QtCore.Qt.Unchecked)
qTable.setItem(row,col,chkBoxItem)
Run Code Online (Sandbox Code Playgroud)
我错过了一些明显的东西吗?
我还检查了以下帖子:
也许这之前被问过,在这种情况下我会删除这个问题,但我有两个列表:
occurence_list = [1, 2, 3, 4, 5]
value_list = [10, 20, 30, 40, 50]
Run Code Online (Sandbox Code Playgroud)
我希望每个值看起来与另一个列表中相同索引的值相同:
result = [10, 20, 20, 30, 30, 30, 40, 40, 40, 40, 50, 50, 50, 50, 50]
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?