从 csv 文件读取的 Python 脚本

发布于 2024-09-06 16:32:52 字数 1113 浏览 3 评论 0原文

           "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 技术交流群。

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

发布评论

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

评论(1

深海里的那抹蓝 2024-09-13 16:32:52

您正在导入 csv 模块,但从未使用它。为什么?

如果您这样做,

import csv
reader = csv.reader(open(file, "rb"), dialect="excel") # Python 2.x
# Python 3: reader = csv.reader(open(file, newline=""), dialect="excel")

您将获得一个包含您需要的所有内容的 reader 对象;第一行将包含标题,后续行将在相应位置包含数据。

更好的可能是(如果我理解正确的话):

import csv
reader = csv.DictReader(open(file, "rb"), dialect="excel") # Python 2.x
# Python 3: reader = csv.DictReader(open(file, newline=""), dialect="excel")

这个 DictReader 可以迭代,返回一系列使用列标题作为键和以下数据的 dict作为值,因此

for row in reader:
    print(row)

将输出

{'Name': 'Nick', 'Designation': 'F4321', 'Type': 'Subject', 'Total': '29', 'First-term assessment': '10', 'Second-term assessment': '19', 'Description': 'D1234'}
{'Name': 'HTML', 'Designation': 'F4321', 'Type': 'Unit', 'Total': '', 'First-term assessment': '18', 'Second-term assessment': '', 'Description': 'D1234-1'}
{'Name': 'Tags', 'Designation': 'F4321', 'Type': 'Topic', 'Total': '', 'First-term assessment': '18', 'Second-term assessment': '', 'Description': 'First Term'}
{'Name': 'Review of representation of HTML', 'Designation': '', 'Type': 'Subtopic', 'Total': '', 'First-term assessment': '', 'Second-term assessment': '', 'Description': ''}

You're importing the csv module but never use it. Why?

If you do

import csv
reader = csv.reader(open(file, "rb"), dialect="excel") # Python 2.x
# Python 3: reader = csv.reader(open(file, newline=""), dialect="excel")

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

import csv
reader = csv.DictReader(open(file, "rb"), dialect="excel") # Python 2.x
# Python 3: reader = csv.DictReader(open(file, newline=""), dialect="excel")

This DictReader can be iterated over, returning a sequence of dicts that use the column header as keys and the following data as values, so

for row in reader:
    print(row)

will output

{'Name': 'Nick', 'Designation': 'F4321', 'Type': 'Subject', 'Total': '29', 'First-term assessment': '10', 'Second-term assessment': '19', 'Description': 'D1234'}
{'Name': 'HTML', 'Designation': 'F4321', 'Type': 'Unit', 'Total': '', 'First-term assessment': '18', 'Second-term assessment': '', 'Description': 'D1234-1'}
{'Name': 'Tags', 'Designation': 'F4321', 'Type': 'Topic', 'Total': '', 'First-term assessment': '18', 'Second-term assessment': '', 'Description': 'First Term'}
{'Name': 'Review of representation of HTML', 'Designation': '', 'Type': 'Subtopic', 'Total': '', 'First-term assessment': '', 'Second-term assessment': '', 'Description': ''}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文