Python将tab受限文件转换为csv
我基本上希望将制表符分隔的文本文件转换为csv文件 我尝试使用Excel导入,但不是最优的,结果是:Python将tab受限文件转换为csv,python,excel,csv,export-to-csv,Python,Excel,Csv,Export To Csv,我基本上希望将制表符分隔的文本文件转换为csv文件 我尝试使用Excel导入,但不是最优的,结果是: 8087 Intel Corp. 0020 Integrated Rate Matching Hub 0024 Integrated Rate Matching Hub 为了便于搜索,我希望它是: 8087 Intel Corp. 0020 Integrated Rate Matching Hub 80
8087 Intel Corp.
0020 Integrated Rate Matching Hub
0024 Integrated Rate Matching Hub
为了便于搜索,我希望它是:
8087 Intel Corp. 0020 Integrated Rate Matching Hub
8087 Intel Corp. 0024 Integrated Rate Matching Hub
在python中有什么方法可以做到这一点吗?您只需要编写一个小程序,一次扫描一行数据。然后它应该检查第一个字符是否是制表符('\t')。如果没有,则应存储该值。如果以tab开头,则打印出之前存储的值,后跟当前行。结果将是您想要的格式的列表。您只需要编写一个小程序,一次扫描一行数据。然后它应该检查第一个字符是否是制表符('\t')。如果没有,则应存储该值。如果以tab开头,则打印出之前存储的值,后跟当前行。结果将是您想要的格式的列表。您只需要编写一个小程序,一次扫描一行数据。然后它应该检查第一个字符是否是制表符('\t')。如果没有,则应存储该值。如果以tab开头,则打印出之前存储的值,后跟当前行。结果将是您想要的格式的列表。您只需要编写一个小程序,一次扫描一行数据。然后它应该检查第一个字符是否是制表符('\t')。如果没有,则应存储该值。如果以tab开头,则打印出之前存储的值,后跟当前行。结果将是您想要的格式的列表。类似这样的方法可以:
import csv
lines = []
with open("usb.ids.txt") as f:
reader = csv.reader(f, delimiter="\t")
device = ""
for line in reader:
# Ignore empty lines and comments
if len(line) == 0 or (len(line[0]) > 0 and line[0][0] == "#"):
continue
if line[0] != "":
device = line[0]
elif line[1] != "":
lines.append((device, line[1]))
print(lines)
你基本上需要在每条线路上循环,如果它是一条设备线路,请记住以下几行。这只适用于两列,然后您需要将它们全部写入csv文件,但这很简单,类似这样的操作可以:
import csv
lines = []
with open("usb.ids.txt") as f:
reader = csv.reader(f, delimiter="\t")
device = ""
for line in reader:
# Ignore empty lines and comments
if len(line) == 0 or (len(line[0]) > 0 and line[0][0] == "#"):
continue
if line[0] != "":
device = line[0]
elif line[1] != "":
lines.append((device, line[1]))
print(lines)
$ListDirectory = "C:\USB_List.csv"
Invoke-WebRequest 'http://www.linux-usb.org/usb.ids' -OutFile $ListDirectory
$pageContents = Get-Content $ListDirectory | Select-Object -Skip 22
"vendor`tvendor_name`tproduct`tproduct_name`r" > $ListDirectory
#Variables and Flags
$currentVid
$currentVName
$currentPid
$currentPName
$vendorDone = $TRUE
$interfaceFlag = $FALSE
$nextline
$tab = "`t"
foreach($line in $pageContents){
if($line.StartsWith("`#")){
continue
}
elseif($line.length -eq 0){
exit
}
if(!($line.StartsWith($tab)) -and ($vendorDone -eq $TRUE)){
$vendorDone = $FALSE
}
if(!($line.StartsWith($tab)) -and ($vendorDone -eq $FALSE)){
$pos = $line.IndexOf(" ")
$currentVid = $line.Substring(0, $pos)
$currentVName = $line.Substring($pos+2)
"$currentVid`t$currentVName`t`t`r" >> $ListDirectory
$vendorDone = $TRUE
}
elseif ($line.StartsWith($tab)){
if ($interfaceFlag -eq $TRUE){
$interfaceFlag = $FALSE
}
$nextline = $line.TrimStart()
if ($nextline.StartsWith($tab)){
$interfaceFlag = $TRUE
}
if ($interfaceFlag -eq $FALSE){
$pos = $nextline.IndexOf(" ")
$currentPid = $nextline.Substring(0, $pos)
$currentPName = $nextline.Substring($pos+2)
"$currentVid`t$currentVName`t$currentPid`t$currentPName`r" >> $ListDirectory
Write-Host "$currentVid`t$currentVName`t$currentPid`t$currentPName`r"
$interfaceFlag = $FALSE
}
}
}
你基本上需要在每条线路上循环,如果它是一条设备线路,请记住以下几行。这只适用于两列,然后您需要将它们全部写入csv文件,但这很简单,类似这样的操作可以:
import csv
lines = []
with open("usb.ids.txt") as f:
reader = csv.reader(f, delimiter="\t")
device = ""
for line in reader:
# Ignore empty lines and comments
if len(line) == 0 or (len(line[0]) > 0 and line[0][0] == "#"):
continue
if line[0] != "":
device = line[0]
elif line[1] != "":
lines.append((device, line[1]))
print(lines)
$ListDirectory = "C:\USB_List.csv"
Invoke-WebRequest 'http://www.linux-usb.org/usb.ids' -OutFile $ListDirectory
$pageContents = Get-Content $ListDirectory | Select-Object -Skip 22
"vendor`tvendor_name`tproduct`tproduct_name`r" > $ListDirectory
#Variables and Flags
$currentVid
$currentVName
$currentPid
$currentPName
$vendorDone = $TRUE
$interfaceFlag = $FALSE
$nextline
$tab = "`t"
foreach($line in $pageContents){
if($line.StartsWith("`#")){
continue
}
elseif($line.length -eq 0){
exit
}
if(!($line.StartsWith($tab)) -and ($vendorDone -eq $TRUE)){
$vendorDone = $FALSE
}
if(!($line.StartsWith($tab)) -and ($vendorDone -eq $FALSE)){
$pos = $line.IndexOf(" ")
$currentVid = $line.Substring(0, $pos)
$currentVName = $line.Substring($pos+2)
"$currentVid`t$currentVName`t`t`r" >> $ListDirectory
$vendorDone = $TRUE
}
elseif ($line.StartsWith($tab)){
if ($interfaceFlag -eq $TRUE){
$interfaceFlag = $FALSE
}
$nextline = $line.TrimStart()
if ($nextline.StartsWith($tab)){
$interfaceFlag = $TRUE
}
if ($interfaceFlag -eq $FALSE){
$pos = $nextline.IndexOf(" ")
$currentPid = $nextline.Substring(0, $pos)
$currentPName = $nextline.Substring($pos+2)
"$currentVid`t$currentVName`t$currentPid`t$currentPName`r" >> $ListDirectory
Write-Host "$currentVid`t$currentVName`t$currentPid`t$currentPName`r"
$interfaceFlag = $FALSE
}
}
}
你基本上需要在每条线路上循环,如果它是一条设备线路,请记住以下几行。这只适用于两列,然后您需要将它们全部写入csv文件,但这很简单,类似这样的操作可以:
import csv
lines = []
with open("usb.ids.txt") as f:
reader = csv.reader(f, delimiter="\t")
device = ""
for line in reader:
# Ignore empty lines and comments
if len(line) == 0 or (len(line[0]) > 0 and line[0][0] == "#"):
continue
if line[0] != "":
device = line[0]
elif line[1] != "":
lines.append((device, line[1]))
print(lines)
$ListDirectory = "C:\USB_List.csv"
Invoke-WebRequest 'http://www.linux-usb.org/usb.ids' -OutFile $ListDirectory
$pageContents = Get-Content $ListDirectory | Select-Object -Skip 22
"vendor`tvendor_name`tproduct`tproduct_name`r" > $ListDirectory
#Variables and Flags
$currentVid
$currentVName
$currentPid
$currentPName
$vendorDone = $TRUE
$interfaceFlag = $FALSE
$nextline
$tab = "`t"
foreach($line in $pageContents){
if($line.StartsWith("`#")){
continue
}
elseif($line.length -eq 0){
exit
}
if(!($line.StartsWith($tab)) -and ($vendorDone -eq $TRUE)){
$vendorDone = $FALSE
}
if(!($line.StartsWith($tab)) -and ($vendorDone -eq $FALSE)){
$pos = $line.IndexOf(" ")
$currentVid = $line.Substring(0, $pos)
$currentVName = $line.Substring($pos+2)
"$currentVid`t$currentVName`t`t`r" >> $ListDirectory
$vendorDone = $TRUE
}
elseif ($line.StartsWith($tab)){
if ($interfaceFlag -eq $TRUE){
$interfaceFlag = $FALSE
}
$nextline = $line.TrimStart()
if ($nextline.StartsWith($tab)){
$interfaceFlag = $TRUE
}
if ($interfaceFlag -eq $FALSE){
$pos = $nextline.IndexOf(" ")
$currentPid = $nextline.Substring(0, $pos)
$currentPName = $nextline.Substring($pos+2)
"$currentVid`t$currentVName`t$currentPid`t$currentPName`r" >> $ListDirectory
Write-Host "$currentVid`t$currentVName`t$currentPid`t$currentPName`r"
$interfaceFlag = $FALSE
}
}
}
你基本上需要在每条线路上循环,如果它是一条设备线路,请记住以下几行。这只适用于两列,然后需要将它们全部写入csv文件,但这很简单
$ListDirectory = "C:\USB_List.csv"
Invoke-WebRequest 'http://www.linux-usb.org/usb.ids' -OutFile $ListDirectory
$pageContents = Get-Content $ListDirectory | Select-Object -Skip 22
"vendor`tvendor_name`tproduct`tproduct_name`r" > $ListDirectory
#Variables and Flags
$currentVid
$currentVName
$currentPid
$currentPName
$vendorDone = $TRUE
$interfaceFlag = $FALSE
$nextline
$tab = "`t"
foreach($line in $pageContents){
if($line.StartsWith("`#")){
continue
}
elseif($line.length -eq 0){
exit
}
if(!($line.StartsWith($tab)) -and ($vendorDone -eq $TRUE)){
$vendorDone = $FALSE
}
if(!($line.StartsWith($tab)) -and ($vendorDone -eq $FALSE)){
$pos = $line.IndexOf(" ")
$currentVid = $line.Substring(0, $pos)
$currentVName = $line.Substring($pos+2)
"$currentVid`t$currentVName`t`t`r" >> $ListDirectory
$vendorDone = $TRUE
}
elseif ($line.StartsWith($tab)){
if ($interfaceFlag -eq $TRUE){
$interfaceFlag = $FALSE
}
$nextline = $line.TrimStart()
if ($nextline.StartsWith($tab)){
$interfaceFlag = $TRUE
}
if ($interfaceFlag -eq $FALSE){
$pos = $nextline.IndexOf(" ")
$currentPid = $nextline.Substring(0, $pos)
$currentPName = $nextline.Substring($pos+2)
"$currentVid`t$currentVName`t$currentPid`t$currentPName`r" >> $ListDirectory
Write-Host "$currentVid`t$currentVName`t$currentPid`t$currentPName`r"
$interfaceFlag = $FALSE
}
}
}
我知道ask是针对python的,但我构建了这个PowerShell脚本来完成这项工作。它不需要参数。只需从要存储脚本的目录中以管理员身份运行即可。脚本收集页面中的所有内容,解析数据并将其写入以制表符分隔的文件。然后,可以在excel中以制表符分隔的文件形式打开该文件。确保列读为“文本”而不是“一般”,然后您就可以开始了。:)
解析此页面很棘手,因为脚本必须在上下文中了解进行一系列PID产品线的每个VID供应商线。我还强迫脚本忽略注释的描述部分、接口-接口名称行、他在整个USB列表(sign)中插入的随机注释以及在“#已知设备类、子类和协议列表”之后的所有内容,这些内容超出了此请求的范围
我希望这有帮助
我知道ask是针对python的,但我构建了这个PowerShell脚本来完成这项工作。它不需要参数。只需从要存储脚本的目录中以管理员身份运行即可。脚本收集页面中的所有内容,解析数据并将其写入以制表符分隔的文件。然后,可以在excel中以制表符分隔的文件形式打开该文件。确保列读为“文本”而不是“一般”,然后您就可以开始了。:)
解析此页面很棘手,因为脚本必须在上下文中了解进行一系列PID产品线的每个VID供应商线。我还强迫脚本忽略注释的描述部分、接口-接口名称行、他在整个USB列表(sign)中插入的随机注释以及在“#已知设备类、子类和协议列表”之后的所有内容,这些内容超出了此请求的范围
我希望这有帮助
我知道ask是针对python的,但我构建了这个PowerShell脚本来完成这项工作。它不需要参数。只需从要存储脚本的目录中以管理员身份运行即可。脚本收集页面中的所有内容,解析数据并将其写入以制表符分隔的文件。然后,可以在excel中以制表符分隔的文件形式打开该文件。确保列读为“文本”而不是“一般”,然后您就可以开始了。:)
解析此页面很棘手,因为脚本必须在上下文中了解进行一系列PID产品线的每个VID供应商线。我还强迫脚本忽略注释的描述部分、接口-接口名称行、他在整个USB列表(sign)中插入的随机注释以及在“#已知设备类、子类和协议列表”之后的所有内容,这些内容超出了此请求的范围
我希望这有帮助
我知道ask是针对python的,但我构建了这个PowerShell脚本来完成这项工作。它不需要参数。只需从要存储脚本的目录中以管理员身份运行即可。脚本收集页面中的所有内容,解析数据并将其写入以制表符分隔的文件。然后,可以在excel中以制表符分隔的文件形式打开该文件。确保列读为“文本”而不是“一般”,然后您就可以开始了。:)
解析这个