im当前正在尝试将csv文件转换为xlsx文件。 我确实在这里找到了很多帮助,但是我有一个主要问题。
我的csv文件将一些数据存储在不同的列中。如果我在python中将该文件转换为xlsx,则所有数据都放在带有“;”的一列中它们之间。 但是如何将这些列保留在xlsx文件中?
如果我打开csv文件并将其另存为xlsx,它也可以正常工作,并且一切保持不变。
编辑:
这就是运行脚本后xlsx文件的外观:
编辑2:
import os
import glob
import csv
from xlsxwriter.workbook import Workbook
for csv_f in glob.glob(os.path.join('.', '*.csv')):
workbook = Workbook(csv_f[:-4] + '.xlsx')
worksheet = workbook.add_worksheet()
with open(csv_f, 'rt', encoding='utf8') as f:
reader = csv.reader(f)
for r, row in enumerate(reader):
for c, col in enumerate(row):
worksheet.write(r, c, col)
workbook.close()
答案 0 :(得分:0)
这就是通过xlsxwriter
import os
import glob
import csv
from xlsxwriter.workbook import Workbook
for csv_f in glob.glob(os.path.join('.', '*.csv')):
workbook = Workbook(csv_f[:-4] + '.xlsx')
worksheet = workbook.add_worksheet()
with open(csv_f, 'rt', encoding='utf8') as f:
reader = csv.reader(f)
for r, row in enumerate(reader):
for c, col in enumerate(row):
worksheet.write(r, c, col)
workbook.close()
答案 1 :(得分:0)
我修复了!
int a = 65535;
int count = 0;
int temp = a;
while(temp != 0)
{
++count;
temp = temp >> 1;
}
int b = 65;
int c = b << count | a;
这里重要的是该行中的import os
import glob
import csv
from xlsxwriter.workbook import Workbook
for csvfile in glob.glob(os.path.join('.', '*.csv')):
workbook = Workbook(csvfile[:-4] + '.xlsx')
worksheet = workbook.add_worksheet()
with open(csvfile, 'rt', encoding='utf8') as f:
reader = csv.reader(f, delimiter=";")
for r, row in enumerate(reader):
for c, col in enumerate(row):
str(worksheet.write(r, c, col)).split(";")
workbook.close()
:
"delimiter=";"