如何重命名文件并更改文件类型?

发布于 2024-12-16 16:15:59 字数 394 浏览 0 评论 0原文

我有一个包含 .dbf 文件的列表,我想将其更改为 .csv 文件。我手动在 Excel 中打开它们并将它们重新保存为 .csv,但这需要太多时间。

现在我制作了一个更改文件名的脚本,但是当我打开它时,它仍然是 .dbf 文件类型(尽管它被称为 .csv)。如何重命名文件以使文件类型也发生变化?

我的脚本使用(dbf 和 csv 文件名列在单独的 csv 文件中):

IN = dbffile name
OUT = csvfile name

for output_line in lstRename:
    shutil.copyfile(IN,OUT)

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.

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?

My script uses (the dbf and csv file name are listed in a seperate csv file):

IN = dbffile name
OUT = csvfile name

for output_line in lstRename:
    shutil.copyfile(IN,OUT)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

裸钻 2024-12-23 16:15:59

更改文件名(扩展名只是完整名称的一部分)对文件的内容绝对没有影响。您需要以某种方式将内容从一种格式转换为另一种格式。

使用我的dbf模块python 这非常简单:

import dbf

IN = 'some_file.dbf'
OUT = 'new_name.csv'

dbf.Table(IN).export(filename=OUT)

这将创建一个实际上是 csv 格式的 .csv 文件。

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:

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.

む无字情书 2024-12-23 16:15:59

如果您曾经使用过 VB 或研究过 VBA,您可以编写一个简单的 Excel 脚本来打开每个文件,将其另存为 csv,然后使用新名称保存。

使用宏录制器录制您自己执行的操作,然后编辑生成的脚本。

我现在创建了一个自动执行此操作的应用程序。它称为 xlsto (查找 xls.exe 发布文件)。它允许您选择一个文件夹并将所有 xls 文件转换为 csv(或任何其他类型)。

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.

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).

恋你朝朝暮暮 2024-12-23 16:15:59

您需要转换器

搜索 dbf2csv 在谷歌中。

You need a converter

Search for dbf2csv in google.

浮华 2024-12-23 16:15:59

这取决于你想做什么。您似乎想将文件转换为其他类型。那里有很多转换器,但仅靠计算机并不能识别每种文件类型。为此,您需要下载一些软件。如果您只想更改文件扩展名,
(例如 .png、.doc、.wav)然后您可以将计算机设置为能够更改名称和扩展名。我希望我能以某种方式提供帮助:)

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 :)

§普罗旺斯的薰衣草 2024-12-23 16:15:59

下载 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

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
左秋 2024-12-23 16:15:59

新文件名可能是“xzy.csv.dbf”。通常在 C# 中我在文件名中添加引号。这会强制操作系统更改文件名。尝试使用引号中的“Xzy.csv”之类的内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文