从 csv 文件读取的 Python 脚本
"Type","Name","Description","Designation","First-term assessment","Second-term assessment","Total"
"Subject","Nick","D1234","F4321",10,19,29
"Unit","HTML","D1234-1","F4321",18,,
"Topic","Tags","First Term","F4321",18,,
"Subtopic","Review of representation of HTML",,,,,
以上所有都是来自 Excel 工作表的值,该值被转换为 csv,即上面显示的值。
您注意到标题包含七个 coulmns,它们下面的数据各不相同,
我有这个脚本从 python 脚本生成这些值,脚本如下
from django.db import transaction
import sys
import csv
import StringIO
file = sys.argv[1]
no_cols_flag=0
flag=0
header_arr=[]
print file
f = open(file, 'r')
while (f.readline() != ""):
for i in [line.split(',') for line in open(file)]: # split on the separator
print "==========================================================="
row_flag=0
row_d=""
for j in i: # for each token in the split string
row_flag=1
print j
if j:
no_cols_flag=no_cols_flag+1
data=j.strip()
print j
break
如何修改上面的脚本以表明该数据属于特定的列标题..
谢谢..
"Type","Name","Description","Designation","First-term assessment","Second-term assessment","Total"
"Subject","Nick","D1234","F4321",10,19,29
"Unit","HTML","D1234-1","F4321",18,,
"Topic","Tags","First Term","F4321",18,,
"Subtopic","Review of representation of HTML",,,,,
All the above are the value from an excel sheet , which is converted to csv and that is the one shown above
The header as you notice contains seven coulmns,the data below them vary,
I have this script to generate these from python script,the script is below
from django.db import transaction
import sys
import csv
import StringIO
file = sys.argv[1]
no_cols_flag=0
flag=0
header_arr=[]
print file
f = open(file, 'r')
while (f.readline() != ""):
for i in [line.split(',') for line in open(file)]: # split on the separator
print "==========================================================="
row_flag=0
row_d=""
for j in i: # for each token in the split string
row_flag=1
print j
if j:
no_cols_flag=no_cols_flag+1
data=j.strip()
print j
break
How to modify the above script to say that this data belongs to a particular column header..
thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在导入
csv
模块,但从未使用它。为什么?如果您这样做,
您将获得一个包含您需要的所有内容的
reader
对象;第一行将包含标题,后续行将在相应位置包含数据。更好的可能是(如果我理解正确的话):
这个
DictReader
可以迭代,返回一系列使用列标题作为键和以下数据的dict
作为值,因此将输出
You're importing the
csv
module but never use it. Why?If you do
you get a
reader
object that will contain all you need; the first row will contain the headers, and the subsequent rows will contain the data in the corresponding places.Even better might be (if I understand you correctly):
This
DictReader
can be iterated over, returning a sequence ofdict
s that use the column header as keys and the following data as values, sowill output