I have a list with .dbf
files which I want to change to .csv
files. By hand I open them in excel and re-save them as .csv
, but this takes too much time.
我有一个包含.dbf文件的列表,我想将其更改为.csv文件。手动我在excel中打开它们并将它们重新保存为.csv,但这需要花费太多时间。
Now I made a script which changes the file name, but when I open it, it is still a .dbf
file type (although it is called .csv
). How can I rename the files in such a way that the file type also changes?
现在我创建了一个更改文件名的脚本,但是当我打开它时,它仍然是.dbf文件类型(虽然它被称为.csv)。如何以文件类型也发生变化的方式重命名文件?
My script uses (the dbf and csv file name are listed in a seperate csv file):
我的脚本使用(dbf和csv文件名列在单独的csv文件中):
IN = dbffile name
OUT = csvfile name
for output_line in lstRename:
shutil.copyfile(IN,OUT)
6 个解决方案
#1
6
Changing the name of a file (and the extension is just part of the complete name) has absolutely no effect on the contents of the file. You need to somehow convert the contents from one format to the other.
更改文件的名称(扩展名只是完整名称的一部分)对文件的内容完全没有影响。您需要以某种方式将内容从一种格式转换为另一种格式。
Using my dbf module and python it is quite simple:
使用我的dbf模块和python非常简单:
import dbf
IN = 'some_file.dbf'
OUT = 'new_name.csv'
dbf.Table(IN).export(filename=OUT)
This will create a .csv
file that is actually in csv format.
这将创建一个实际为csv格式的.csv文件。
#2
2
If you have ever used VB or looked into VBA, you can write a simple excel script to open each file, save it as csv and then save it with a new name.
如果您曾经使用过VB或查看过VBA,您可以编写一个简单的Excel脚本来打开每个文件,将其保存为csv,然后使用新名称保存。
Use the macro recorder to record you once doing it yourself and then edit the resulting script.
一旦自己动手,请使用宏录制器录制您,然后编辑生成的脚本。
I have now created a application that automates this. Its called xlsto (look for the xls.exe release file). It allows you to pick a folder and convert all xls files to csv (or any other type).
我现在创建了一个自动执行此操作的应用程序。它叫做xlsto(查找xls.exe发布文件)。它允许您选择一个文件夹并将所有xls文件转换为csv(或任何其他类型)。
#4
0
It depends what you want to do. It seems like you want to convert files to other types. There are many converters out there, but a computer alone doesn't know every file type. For that you will need to download some software. If all you want to do is change the file extension, (ex. .png, .doc, .wav) then you can set your computer to be able to change both the name and the extension. I hoped I helped in some way :)
这取决于你想做什么。您似乎想要将文件转换为其他类型。那里有很多转换器,但仅有一台计算机并不知道每种文件类型。为此,您需要下载一些软件。如果您只想更改文件扩展名(例如.png,.doc,.wav),则可以将计算机设置为能够更改名称和扩展名。我希望我能以某种方式帮助:)
#5
-1
descargar libreria dbfpy desde http://sourceforge.net/projects/dbfpy/?source=dlp
descargar libreria dbfpy desde http://sourceforge.net/projects/dbfpy/?source=dlp
import csv,glob
from dbfpy import dbf
entrada = raw_input(" entresucarpetadbf ::")
lisDbf = glob.glob(entrada + "\\*dbf")
for db in lisDbf:
print db
try:
dbfFile = dbf.Dbf(open(db,'r'))
csvFile = csv.writer(open(db[:-3] + "csv", 'wb'))
headers = range(len(dbfFile.fieldNames))
allRows = []
for row in dbfFile:
rows = []
for num in headers:
rows.append(row[num])
allRows.append(rows)
csvFile.writerow(dbfFile.fieldNames)
for row in allRows:
print row
csvFile.writerow(row)
except Exception,e:
print e
#6
-2
It might be that the new file name is "xzy.csv.dbf". Usually in C# I put quotes in the filename. This forces the OS to change the filename. Try something like "Xzy.csv" in quotes.
可能是新文件名是“xzy.csv.dbf”。通常在C#中我在文件名中加上引号。这会强制操作系统更改文件名。在引号中尝试类似“Xzy.csv”的内容。
#1
6
Changing the name of a file (and the extension is just part of the complete name) has absolutely no effect on the contents of the file. You need to somehow convert the contents from one format to the other.
更改文件的名称(扩展名只是完整名称的一部分)对文件的内容完全没有影响。您需要以某种方式将内容从一种格式转换为另一种格式。
Using my dbf module and python it is quite simple:
使用我的dbf模块和python非常简单:
import dbf
IN = 'some_file.dbf'
OUT = 'new_name.csv'
dbf.Table(IN).export(filename=OUT)
This will create a .csv
file that is actually in csv format.
这将创建一个实际为csv格式的.csv文件。
#2
2
If you have ever used VB or looked into VBA, you can write a simple excel script to open each file, save it as csv and then save it with a new name.
如果您曾经使用过VB或查看过VBA,您可以编写一个简单的Excel脚本来打开每个文件,将其保存为csv,然后使用新名称保存。
Use the macro recorder to record you once doing it yourself and then edit the resulting script.
一旦自己动手,请使用宏录制器录制您,然后编辑生成的脚本。
I have now created a application that automates this. Its called xlsto (look for the xls.exe release file). It allows you to pick a folder and convert all xls files to csv (or any other type).
我现在创建了一个自动执行此操作的应用程序。它叫做xlsto(查找xls.exe发布文件)。它允许您选择一个文件夹并将所有xls文件转换为csv(或任何其他类型)。
#3
#4
0
It depends what you want to do. It seems like you want to convert files to other types. There are many converters out there, but a computer alone doesn't know every file type. For that you will need to download some software. If all you want to do is change the file extension, (ex. .png, .doc, .wav) then you can set your computer to be able to change both the name and the extension. I hoped I helped in some way :)
这取决于你想做什么。您似乎想要将文件转换为其他类型。那里有很多转换器,但仅有一台计算机并不知道每种文件类型。为此,您需要下载一些软件。如果您只想更改文件扩展名(例如.png,.doc,.wav),则可以将计算机设置为能够更改名称和扩展名。我希望我能以某种方式帮助:)
#5
-1
descargar libreria dbfpy desde http://sourceforge.net/projects/dbfpy/?source=dlp
descargar libreria dbfpy desde http://sourceforge.net/projects/dbfpy/?source=dlp
import csv,glob
from dbfpy import dbf
entrada = raw_input(" entresucarpetadbf ::")
lisDbf = glob.glob(entrada + "\\*dbf")
for db in lisDbf:
print db
try:
dbfFile = dbf.Dbf(open(db,'r'))
csvFile = csv.writer(open(db[:-3] + "csv", 'wb'))
headers = range(len(dbfFile.fieldNames))
allRows = []
for row in dbfFile:
rows = []
for num in headers:
rows.append(row[num])
allRows.append(rows)
csvFile.writerow(dbfFile.fieldNames)
for row in allRows:
print row
csvFile.writerow(row)
except Exception,e:
print e
#6
-2
It might be that the new file name is "xzy.csv.dbf". Usually in C# I put quotes in the filename. This forces the OS to change the filename. Try something like "Xzy.csv" in quotes.
可能是新文件名是“xzy.csv.dbf”。通常在C#中我在文件名中加上引号。这会强制操作系统更改文件名。在引号中尝试类似“Xzy.csv”的内容。