Python CSV不会将数据写入文件(Python CSV not writing data to file)
我正在碰壁。 我是使用python编写CSV文件的新手,并且已经阅读了很多关于该主题的不同帖子,但现在我遇到了这个并且可以使用一点帮助。
import csv #headers from the read.csv file that I wan't to parse and write to the new file. headers = ['header1', 'header5', 'header6', 'header7'] #open the write.csv file to write the data to with open("write.csv", 'wb') as csvWriter: writer = csv.writer(csvWriter) #open the main data file that I want to parse data out of and write to write.csv with open('reading.csv') as csvfile: readCSV = csv.reader(csvfile, delimiter=',' ) csvList = list(readCSV) #finds where the position of the data I want to pull out and write to write.csv itemCode = csvList[0].index(headers[0]) vendorName = csvList[0].index(headers[1]) supplierID = csvList[0].index(headers[2]) supplierItemCode = csvList[0].index(headers[3]) for row in readCSV: writer.writerow([row[itemCode], row[vendorName], row[supplierID], row[supplierItemCode]]) csvWriter.close()
---更新---我做了修改建议并尝试评论代码的以下部分并将'wb'更改为'w'并且程序正常运行。 但是,我不明白为什么,以及如何设置它以便我可以列出我想要提取的标题?
csvList = list(readCSV) itemCode = csvList[0].index(headers[0]) vendorName = csvList[0].index(headers[1]) supplierID = csvList[0].index(headers[2]) supplierItemCode = csvList[0].index(headers[3])
这是我更新的代码:
headers = ['header1', 'header5', 'header6', 'header7'] #open the write.csv file to write the data to with open("write.csv", 'wb') as csvWriter, open('reading.csv') as csvfile: writer = csv.writer(csvWriter) readCSV = csv.reader(csvfile, delimiter=',' ) """csvList = list(readCSV) #finds where the position of the data I want to pull out and write to write.csv itemCode = csvList[0].index(headers[0]) vendorName = csvList[0].index(headers[1]) supplierID = csvList[0].index(headers[2]) supplierItemCode = csvList[0].index(headers[3])""" for row in readCSV: writer.writerow([row[0], row[27], row[28], row[29]])
I am running into a wall with this. I am new to writing CSV files with python and have been reading lots of different posts on the topic, but now I ran into a wall with this and could use a little help.
import csv #headers from the read.csv file that I wan't to parse and write to the new file. headers = ['header1', 'header5', 'header6', 'header7'] #open the write.csv file to write the data to with open("write.csv", 'wb') as csvWriter: writer = csv.writer(csvWriter) #open the main data file that I want to parse data out of and write to write.csv with open('reading.csv') as csvfile: readCSV = csv.reader(csvfile, delimiter=',' ) csvList = list(readCSV) #finds where the position of the data I want to pull out and write to write.csv itemCode = csvList[0].index(headers[0]) vendorName = csvList[0].index(headers[1]) supplierID = csvList[0].index(headers[2]) supplierItemCode = csvList[0].index(headers[3]) for row in readCSV: writer.writerow([row[itemCode], row[vendorName], row[supplierID], row[supplierItemCode]]) csvWriter.close()
---UPDATE--- I made the changes suggested and tried commenting out the following part of the code & changing 'wb' to 'w' and the program worked. However, I don't understand why, and how do I set this up so that I can list the header I want to pull out?
csvList = list(readCSV) itemCode = csvList[0].index(headers[0]) vendorName = csvList[0].index(headers[1]) supplierID = csvList[0].index(headers[2]) supplierItemCode = csvList[0].index(headers[3])
Here is my updated code:
headers = ['header1', 'header5', 'header6', 'header7'] #open the write.csv file to write the data to with open("write.csv", 'wb') as csvWriter, open('reading.csv') as csvfile: writer = csv.writer(csvWriter) readCSV = csv.reader(csvfile, delimiter=',' ) """csvList = list(readCSV) #finds where the position of the data I want to pull out and write to write.csv itemCode = csvList[0].index(headers[0]) vendorName = csvList[0].index(headers[1]) supplierID = csvList[0].index(headers[2]) supplierItemCode = csvList[0].index(headers[3])""" for row in readCSV: writer.writerow([row[0], row[27], row[28], row[29]])
原文:https://stackoverflow.com/questions/44792921
最满意答案
有一个可可豆荚问题,命名不好。 我报告了这个问题,很快就会解决这个问题
There was an cocoa pods issue with bad naming. I reported it and this issue will be fixed soon
相关问答
更多-
我通过运行以下命令解决了这个问题: sudo chown -R username:groupname ~/Library/Caches/CocoaPods 和 sudo chown -R username:groupname ~/.cocoapods 请用您的Mac登录用户名/组username替换username和组名。 I solved this problem by running the following command: sudo chown -R username:groupname ~/ ...
-
Pod安装在cocoapods版本1.0.0.beta.1中显示错误(Pod install displaying error in cocoapods version 1.0.0.beta.1)[2022-05-05]
您必须为每个pod指定一个目标。 例如,如果在你的Podfile之前写的如下: pod 'Alamofire', '~> 3.1.4' pod 'SwiftyJSON', '~> 2.3.2' 只是改变它 target "TargetName" do pod 'Alamofire', '~> 3.1.4' pod 'SwiftyJSON', '~> 2.3.2' end You have to specify a target for each pod. e.g. if before y ... -
成为别人有这个错误。 问题是我在错误的目录中,并试图将一个带有导向到'MyApp.xcodeproj'的目录的Podfile。 这是不正确的,因为Podfile应该只在文件夹中。 最初我无法找到这个文件夹(即使它在我的桌面上),所以我在终端中使用: cd〜/桌面然后 cd〜/ Desktop / MyApp,然后pod init并编辑Podfile,然后进行pod安装... Incase someone else is having this error. The problem was that I wa ...
-
有一个可可豆荚问题,命名不好。 我报告了这个问题,很快就会解决这个问题 There was an cocoa pods issue with bad naming. I reported it and this issue will be fixed soon
-
firebase pod安装的版本比最新版本少(firebase pod was installed less version than the latest version exists)[2023-05-10]
您的Podfile正在使用已弃用的“Google / SignIn”,它会强制使用Firebase 3.x依赖项。 将其更改为 pod 'GoogleSignIn' Your Podfile is using the deprecated 'Google/SignIn' which forces the Firebase 3.x dependencies. Change it to pod 'GoogleSignIn' -
有同样的问题,我相信有更好的方法,但我只是做到了: - (IBAction)revealMenu:(id)sender { ECSlidingViewController *slidingViewController = self.slidingViewController; if (slidingViewController.currentTopViewPosition == ECSlidingViewControllerTopViewPositionAnchoredRight) { ...
-
我的终端上的“pod setup”命令出错(Cocoapods安装)(Error on “pod setup” command on my terminal(Cocoapods Installation))[2022-06-13]
它看起来像Xcode版本的路径被破坏了。 你可以看到路径设置为: xcode-select --print-path 您可能会看到/Volumes/Xcode/Xcode.app/Contents/Developer ,它们必须不存在。 您需要设置路径: xcode-select --switch /path/to/xcode It looks like the path to your version of Xcode is broken. You can see what the path is s ... -
更多的评论而不是答案,但我没有足够的评论声望: 当你使用your-project.xcodeproject pod install时,它会创建一个名为your-project.xcodeworkspace的新文件/文件夹( …project into …workspace ),这是你应该打开的这个新文件。 你没有提到你是否知道这一点,所以在这里:关闭Xcode中的.xcodeproject并打开.xcodeworkspace它应该可以工作。 More a comment than an answer, bu ...
-
该组中的其他人在他们的分支上有不同的库版本。 因此,当我切换到那些分支时,我必须安装它们使用的相同库版本。 Other people in the group had different library versions on their branch. Therefore I have to install the same library versions they are using when I switch to those branches.
-
Pod安装问题(Pod install issue)[2023-09-01]
AdMob现在采用不同的规范,由Google维护: -> Google-Mobile-Ads-SDK (6.6.1) Monetize your mobile applications with Google ads pod 'Google-Mobile-Ads-SDK', '~> 6.6.1' - Homepage: https://developers.google.com/mobile-ads-sdk/ - Source: http://dl.google.com/goog ...