【发布时间】:2015-08-04 15:39:26
【问题描述】:
我正在尝试创建一个程序来替换 Excel 以创建 CSV(供个人使用)。然而,下面的代码复制了第一个文本文件中的值。例如,我拥有的第一个文本文件是“SourceCAS Numbers”。
压缩时会产生以下内容
109922, 109922
当它应该执行以下操作时:
71751412, Abamectin
这是我的代码:
import os import csv import time
#----Federal/State----#
#Ask the user to fill out a text file with Source CAS numbers
print("Input the source CAS numbers with which you're comparing against")
os.system("notepad.exe C:\sourcecas.txt")
time.sleep(15)
#Take the text file with Source CAS Numbers and make it into a list
sourcecas = []
file = open(r'C:\sourcecas.txt', 'r')
sourcecas = file.readlines()
sourcecas[:] = [line.rstrip('\n') for line in sourcecas]
#Ask the user to fill out a text file with Source CAS names
print("Input the source CAS names in alphabetical order")
os.system("notepad.exe C:\sourcecasnames.txt")
time.sleep(15)
#Take the text file with Source CAS Names and make it into a list
sourcecasnames = []
file = open(r'C:\sourcecas.txt', 'r')
sourcecasnames = file.readlines()
sourcecasnames[:] = [line.rstrip('\n') for line in sourcecasnames]
#Zip the source cas numbers and names into a CSV file
zip(sourcecas, sourcecasnames)
with open(r'C:\CAS-S.csv', 'w') as f:
writer = csv.writer(f, delimiter=',')
writer.writerows(zip(sourcecas, sourcecasnames))
【问题讨论】: