我正在编写一个与Perl中的文档一起工作的程序,并且许多文档都有诸如ä, ö, ü, é, etc(大写和小写)之类的字符.我想用ASCII版本替换它们a, o, u, e, etc.我怎么在Perl中做到这一点?
我想到的一个解决方案是使用键是变音符号和重音字符的哈希值,并且值是ASCII对应物,但是这需要我有一个所有变音符号和重音符号的列表,我没有,如果我建立了一个列表,我肯定会想念很多,因为我不熟悉可能有变音符号,重音符号和其他变音符号的所有可能字符.
我正在运行以下Powershell脚本,将一系列输出文件连接到一个CSV文件中.whidataXX.htm(其中xx是两位数的序号),创建的文件数因运行而异.
$metadataPath = "\\ServerPath\foo"
function concatenateMetadata {
$cFile = $metadataPath + "whiconcat.csv"
Clear-Content $cFile
$metadataFiles = gci $metadataPath
$iterations = $metadataFiles.Count
for ($i=0;$i -le $iterations-1;$i++) {
$iFile = "whidata"+$i+".htm"
$FileExists = (Test-Path $metadataPath$iFile -PathType Leaf)
if (!($FileExists))
{
break
}
elseif ($FileExists)
{
Write-Host "Adding " $metadataPath$iFile
Get-Content $metadataPath$iFile | Out-File $cFile -append
Write-Host "to" $cfile
}
}
}
Run Code Online (Sandbox Code Playgroud)
该whidataXX.htm文件进行编码UTF8,但我的输出文件进行编码UTF-16.当我在记事本中查看文件时,它看起来是正确的,但是当我在十六进制编辑器中查看它时,Hex值00出现在每个字符之间,当我将文件拉入Java程序进行处理时,文件将打印到控制台额外的空间c h a r a c t e r s.
首先,这对PowerShell来说是正常的吗?或者源文件中是否存在导致此问题的内容? …
我想在python中使用Decimal()数据类型并将其转换为整数和指数,以便我可以将数据发送到具有全精度和十进制控制的微控制器/ plc.https://docs.python.org/2/library/decimal.html
我有它工作,但它是hackish; 有谁知道更好的方法?如果不是我自己用什么路径写一个较低级别的"as_int()"函数?
示例代码:
from decimal import *
d=Decimal('3.14159')
t=d.as_tuple()
if t[0] == 0:
sign=1
else:
sign=-1
digits= t[1]
theExponent=t[2]
theInteger=sign * int(''.join(map(str,digits)))
theExponent
theInteger
Run Code Online (Sandbox Code Playgroud)
对于没有编程PLC的人,我的替代方法是使用int并在两个系统中声明小数点或使用浮点(只有一些PLC支持)并且是有损的.所以你可以看到为什么能够这样做会很棒!
提前致谢!
也许这不属于SO,但我不知道在哪里.
我不得不重新实现printf(3)与C不使用,会做的转换对我有什么功能,我几乎做了,但我坚持上%a,我真的不明白发生了什么位置,例如:
printf("%a\n", 3.0); //#=> 0x1.8p+1
printf("%a\n", 3.1); //#=> 0x1.8cccccccccccdp+1
printf("%a\n", 3.2); //#=> 0x1.999999999999ap+1
printf("%a\n", 3.3); //#=> 0x1.a666666666666p+1
printf("%a\n", 3.4); //#=> 0x1.b333333333333p+1
printf("%a\n", 3.5); //#=> 0x1.cp+1
printf("%a\n", 3.6); //#=> 0x1.ccccccccccccdp+1
Run Code Online (Sandbox Code Playgroud)
我当然读到了那个说:
double参数被舍入并在样式[ - ] 0xh.hhhp [+ - ] d中转换为十六进制表示法,其中十六进制点字符后面的位数等于精度规范.
但是,这并不能真正帮助我不明白的过程是转换3.2到1.999999999999ap+1
我不需要任何代码,但更多的解释.
PS:如果这不是这个问题的地方,你可以指引我到正确的地方吗?
编辑:虽然@juhist答案适用于数字> = 1.0它没有解释如何获得0.0et 之间的数字结果1.0:
printf("%a\n", 0.01); //#=> 0x1.47ae147ae147bp-7
printf("%a\n", 0.1); //#=> 0x1.999999999999ap-4
printf("%a\n", 0.2); //#=> 0x1.999999999999ap-3
printf("%a\n", 0.3); //#=> 0x1.3333333333333p-2 …Run Code Online (Sandbox Code Playgroud) 我正在与一个拥有现有系统的客户合作,该系统建立在一个显然是Paradox数据库的基础之上.我有一个包含.DB,.MB和.PX文件的zip文件形式的数据库,每个表一个.
我需要获取(一些)这些数据并将其导入到使用MySQL的Web应用程序中.有没有人让我提取这些数据,这不涉及安装Paradox?
如果没有,Paradox是否以某种可读格式导出?无论是SQL还是可以轻松解析的东西?我的客户负责这个系统的人是一名志愿者(他们是一个非盈利组织),所以我想找一个解决方案给他 - 因为上次我要求提供数据,我得到了这个,显然没有好处.
我有一个捕获所有异常的函数,我希望能够在此函数中将回溯作为字符串.
到目前为止,这不起作用:
def handle_errors(error_type, error_message, error_traceback):
"""catch errors"""
import traceback
error = {}
error['type'] = error_type.__name__
error['message'] = str(error_message)
error['file'] = os.path.split(error_traceback.tb_frame.f_code.co_filename)[1]
error['line'] = error_traceback.tb_lineno
error['traceback'] = repr(traceback.print_tb(error_traceback))
### finalise error handling and exit ###
sys.excepthook = handle_errors
Run Code Online (Sandbox Code Playgroud)
这error['traceback']是错误的界限.我甚至需要使用该traceback模块吗?
根据另一个模糊相似的问题,我试过:
error['traceback'] = repr(error_traceback.print_exc())
Run Code Online (Sandbox Code Playgroud)
...但这会产生错误:
Error in sys.excepthook:
Traceback (most recent call last):
File "xxxxxxxxxxx", line 54, in handle_errors
error['traceback'] = repr(error_traceback.print_exc())
AttributeError: 'traceback' object has no attribute 'print_exc'
Run Code Online (Sandbox Code Playgroud) 在我的数据库中,我有一些列,其中数据以某种奇怪的格式存储.由于数据库也被其他代码使用,我无法更改数据格式.
例如,一种奇怪的格式是时间值表示为字符串23:42:30.我想有一些魔法可以让我总是datetime.time在python端使用对象.
一个非常简单的解决方案是:
col_raw = Column('col', String(7))
@property
def col(self):
return datetime.strptime(self.col_raw, '%H:%M:%S').time()
@col.setter
def colself, t):
self.col_raw = t.strftime('%H:%M:%S')
Run Code Online (Sandbox Code Playgroud)
但是,这只能解决读写数据的问题.像这样的东西是不可能的:
Table.query.filter(Table.col == time(23,42,30))
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用混合扩展.这样,查询就有可能,但如果我看得正确,写不会.此外,它需要编写两次编码,一次是在python代码中,一次是在SQL代码中.(其他问题:我不确定转换是否只能使用SQL代码编写.)
真的没有办法将两者结合起来吗?比如我定义了两个功能,比方说python2sql和sql2python,这SQLAlchemy的使用从Python对象的值转换透明地向字符串和后面?我知道,这会使一些查询不可能的,比如between或like或款项之类的,不过没关系.
请记住,时间只是我需要转换数据的情况之一; 所以请尽量保持回复的通用性.
我正在尝试在 jpeg 中转换 heic 文件,同时导入所有元数据(如 GPS 信息和其他内容),不幸的是,转换下面的代码没问题,但创建的 jpeg 文件中没有存储任何元数据。任何人都可以描述我需要在转换方法中添加什么?
heif_file = pyheif.read("/transito/126APPLE_IMG_6272.HEIC")
image = Image.frombytes(
heif_file.mode,
heif_file.size,
heif_file.data,
"raw",
heif_file.mode,
heif_file.stride,
)
image.save("/transito/126APPLE_IMG_6272.JPEG", "JPEG")
Run Code Online (Sandbox Code Playgroud) 我有一个工作正常的程序,但当我把它变成可执行文件时PyInstaller,我有问题.我将其追溯到一个表明问题的奇怪行为.在main(),我发表以下声明:
print float('1e-07')
Run Code Online (Sandbox Code Playgroud)
当我运行我的普通Python脚本时,它'1e-07'按预期打印.
当我运行我的PyInstaller .exe程序时,它会打印出来'ERR'.
有谁知道为什么会这样?
NOTE: This is not some esoteric problem I created. I have a rather large application that frequently converts strings to floats. I found that reading in previously created projects doesn't work anymore and I managed to debug this down to this one effect. I should note that sometimes it converts the numbers properly (in my program) and sometimes not. Everything else runs fine …
我想知道是否有人可以解释如何通过命令行传递参数?我真的很困惑它是如何工作的。现在我正试图将一个整数传递给主程序。我该怎么做呢?
编辑:继续从指针获取初始化使整数没有强制转换 [-Wint-conversion] 错误?
#include <stdio.h>
#define PI 3.1416
int
main (int argc, char *argv[])
{
double r,area, circ;
char a = argv[1];
int num = a - '0';
printf("You have entered %d",num);
r= num/2;
area = PI * r * r;
circ= 2 * PI * r;
printf ("A circle with a diameter of %d ", num);
printf ("has an area of %5.3lf cm2\n", area);
printf ("and a circumference of %4.2lf cm.\n", circ);
return (0);
}
Run Code Online (Sandbox Code Playgroud) data-conversion ×10
python ×5
c ×2
ascii ×1
character ×1
command-line ×1
decimal ×1
diacritics ×1
exif ×1
heic ×1
hex ×1
integer ×1
jpeg ×1
mysql ×1
orm ×1
paradox ×1
perl ×1
plc ×1
powershell ×1
printf ×1
pyinstaller ×1
sqlalchemy ×1
string ×1
traceback ×1
utf-16 ×1
utf-8 ×1