Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何用新的时间戳重写文件中的某些行_Python_Powershell - Fatal编程技术网

Python 如何用新的时间戳重写文件中的某些行

Python 如何用新的时间戳重写文件中的某些行,python,powershell,Python,Powershell,我有一个文件,它使用应用程序的几个不同API版本 在API的V1中,字符串中有始终与模式匹配的开始和结束时间戳 &startTimestamp=1572580801000&endTimestamp=1572667141000` 所以一个完整的url可能看起来像这样(为了安全起见省略了一些数据) 我在这个文件中有一些行没有时间戳。 我每天在同一时间运行这个CURL语句文件。 所以我知道它总是可以增加86400000的固定值作为开始和结束时间戳 如何循环浏览文件,修改具有新的开始和

我有一个文件,它使用应用程序的几个不同API版本 在API的V1中,字符串中有始终与模式匹配的开始和结束时间戳

&startTimestamp=1572580801000&endTimestamp=1572667141000`
所以一个完整的url可能看起来像这样(为了安全起见省略了一些数据)

我在这个文件中有一些行没有时间戳。 我每天在同一时间运行这个CURL语句文件。 所以我知道它总是可以增加86400000的固定值作为开始和结束时间戳

如何循环浏览文件,修改具有新的开始和结束时间戳增量值的行,并使用相同的名称写入文件? 我假设问题的一部分是更改字符串值并将其作为一个整数添加,然后重新构造字符串

下面是一个包含3个条目的文件内容示例

curl -k -X GET "https://serverpathandurl/api/v1/path?query=SELECT%%20xxx%%2C%%20%%20count(xxx)%%20FROM%%20xxx%%20group%%20by%%20xxx&startTimestamp=1572580801000&endTimestamp=1572667141000&explain=false" -H "header " -H "header" > File
curl -k -X GET "https://example2/api/v1/path?query=SELECT%%20xxx%%2C%%20%%20count(xxx)%%20FROM%%20xxx%%20group%%20by%%20xxx&startTimestamp=1572580801000&endTimestamp=1572667141000&explain=false" -H "header " -H "header" > File
curl -k -X GET "https://serverpathandurl/api/v2/metrics/series/xxx?resolution=INF&from=now-1d%%2Fm&scope=entity(xxx)" -H "headerionfo" -H "headerinfo" > file

这个答案适用于PowerShell

URL中的时间戳为Unix毫秒

要通过每天添加一天(86400000)来替换时间戳,可以执行以下操作:

$file  = 'D:\urls.txt'  # your file goes here
$lines = Get-Content -Path $file
for ($i = 0; $i -lt $lines.Count; $i++) {
    if ($lines[$i] -match 'startTimestamp=(\d+)') {
        $newTimeStamp = 'startTimestamp={0}' -f ([int64]$matches[1] + 86400000)
        $lines[$i] = $lines[$i] -replace $matches[0], $newTimeStamp
    }
    if ($lines[$i] -match 'endTimestamp=(\d+)') {
        $newTimeStamp = 'endTimestamp={0}' -f ([int64]$matches[1] + 86400000)
        $lines[$i] = $lines[$i] -replace $matches[0], $newTimeStamp
    }
}

$lines | Set-Content -Path $file -Force
如果希望能够设置自己的开始和结束时间戳,可以使用以下转换方法:

要将Unix毫秒时间戳转换为datetime对象,请执行以下操作:

$utcDate = [DateTimeOffset]::FromUnixTimeMilliseconds(1572667141000).UtcDateTime    # gives you the date in UTC

要将DateTime对象转换为Unix毫秒时间戳,请执行以下操作:

# example uses the current date
[DateTime]$origin = [DateTime]::new(1970, 1, 1, 0, 0, 0, 0, 'Utc')
[TimeSpan]$diff = (Get-Date).ToUniversalTime() - $origin
$unixTimeStamp  = [int64][Math]::Floor($diff.TotalMilliseconds)  # in MilliSeconds
在您的示例中,
startTimestamp
转换为
2019-11-01 04:00:01
endTimestamp
转换为
2019-11-02 03:59:01
(Utc)

startTimestamp和endTimestamp之间的差异是86340000毫秒(1天减去1分钟)

您考虑过使用正则表达式替换吗?我不懂Python,但使用PowerShell应该相当简单。谢谢!这正是我想要完成的。所以你知道。我回去查看了一些带有$matches的正则表达式功能。这将在将来对我有所帮助,我将尽可能以你给出的例子为基础。最亲切的问候@runatyr感谢您的反馈!
$date = [DateTimeOffset]::FromUnixTimeMilliseconds(1572667141000).LocalDateTime  # gives you the date in Local time
# example uses the current date
[DateTime]$origin = [DateTime]::new(1970, 1, 1, 0, 0, 0, 0, 'Utc')
[TimeSpan]$diff = (Get-Date).ToUniversalTime() - $origin
$unixTimeStamp  = [int64][Math]::Floor($diff.TotalMilliseconds)  # in MilliSeconds