简体   繁体   English

当我从Python将数据导出到csv时,该文件为空白。 当我使用打印功能查看数据时,将返回第一个字符串

[英]When I export data to a csv from Python, the file is blank. When I use Print function to see the data, the first string is returned

I am very new to python. 我是python的新手。 I am looking to import data from a .csv into python, use the fields as a variable so that I can string together the inbound data with machine language codes and then export the completed string to a .txt file. 我希望将数据从.csv导入python,将字段用作变量,以便我可以将输入的数据与机器语言代码串在一起,然后将完整的字符串导出到.txt文件。

I have the import.csv code working and have defined my variables into python and when I print the results I can see the first line as I need it to output to the .txt file. 我有import.csv代码,并且已将变量定义为python,当我打印结果时,可以看到第一行,因为我需要将其输出到.txt文件。 The issue that I am having is that in trying multiple different write statements, my file is either blank or I get errors. 我遇到的问题是,在尝试多个不同的写语句时,我的文件为空或出现错误。

import csv

with open('july 19.csv',mode='r') as csv_file:
    reader = csv.DictReader(csv_file)
    for records in reader:
            EIN = records['EIN']
            DATE = records['Date Established']
            DUNS = records['DUNS #']
            COMPANYNAME = records['Company Name']
            lineout =('<S>'+ EIN+'$EIN '+EIN+'*'+DATE+')'+COMPANYNAME+'#D-U-N-S '+DUNS)
with open('test2.csv','w') as csvfile:
        fieldnames = ['EIN','Date Established','DUNS #','Company Name']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writerows({'EIN' : lineout})
  File "C:/Users/sstibal/PycharmProjects/Training/venv/Training.py", line 14, in <module>
    writer.writerows({'EIN' : lineout})
  File "C:\Users\sstibal\AppData\Local\Programs\Python\Python37-32\lib\csv.py", line 158, in writerows
    return self.writer.writerows(map(self._dict_to_list, rowdicts))
  File "C:\Users\sstibal\AppData\Local\Programs\Python\Python37-32\lib\csv.py", line 148, in _dict_to_list
    wrong_fields = rowdict.keys() - self.fieldnames
AttributeError: 'str' object has no attribute 'keys'

You're missing the fieldnames parameter on writer = csv.DictWriter(f, fieldnames) 您在writer = csv.DictWriter(f, fieldnames)上缺少fieldnames参数

The fieldnames parameter is a sequence of keys that identify the order in which values in the dictionary passed to the writerow() method are written to file f. fieldnames参数是键序列,用于标识字典中传递给writerow()方法的值写入文件f的顺序。

docs docs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我使用这个 python 代码时,为什么我的 csv 文件是空白的? 确实无法从中获取数据 - Why my csv file comes blank when I use this python code? can not get data from indeed 当我使用 Python 将列表转换为 CSV 文件时,为什么我的数据之间有空白行? - Why there are blank rows between my data when I converted list to CSV file using Python? 如何将数据从 Python 导出到 a.csv 文件? - How can I export data from Python to a .csv file? 使用Scrapy,当没有数据返回时如何输入空白字符串? - Using Scrapy, how do I enter a blank string when no data is returned? Python 解释 '.' 作为从 csv 文件读取数字数据时的字符串,我想对浮点变量进行数学运算 - Python interprets '.' as a string when reading numeric data from a csv file, I want to do math operations on float variables 当我从 Python 中的 utf-8 文件打印文本时,为什么看不到希伯来语字符? - Why don't I see the hebrew characters, when I print text from an utf-8 file in Python? 当我从 csv 提取数据时,python 中的日期结构发生变化 - Date structure changing in python when I extract data from csv Python:将CSV数据添加到列表并打印时如何删除引号 - Python: How to remove quotes when I add csv data to the list and print it 我想使用 Python 从 csv 文件打印经度和纬度数据 - I want to print longitude and latitude data from a csv file using Python Python将csv数据导出到文件中 - Python export csv data into file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM