作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 python 脚本来清理 csv 文件。脚本文件“CleanCSV.py”中的代码如下
import csv
filepath_i = 'C:\Source Files\Data Source\Flat File Source\PatientRecords.csv'
filepath_o = 'C:\Python\PatientRecords.csv'
rows = []
with open(filepath_i, 'r', newline='') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"')
with open(filepath_o, 'w', newline='' ) as writeFile:
writer = csv.writer(writeFile, lineterminator='\r')
for row in csv_reader:
row[3] = row[3].replace("\n","").replace("\r","")
rows.append(row)
writer.writerows(rows)
从 python 编辑器运行时工作正常。但从命令行运行时不会创建文件,如下所示。
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64>python C:\Python\CleanCSV.py
我也尝试过这个
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64>pythonw C:\Python\CleanCSV.pyw
我提供了对文件夹的完全访问权限,但仍然没有在目标位置创建任何文件。如果我遗漏了什么,请告诉我。
还请建议是否可以优化此代码。我不能使用像 pandas 这样的外部包,所以我用 csv 来做。提前致谢。
Extension
当我将写入设置更改为 x 时
with open(filepath_o, 'x', newline='' ) as writeFile:
令我惊讶的是我收到了这个错误
File "CleanCSV.py", line 8, in <module>
with open(filepath_o, 'x', newline='' ) as writeFile:
FileExistsError: [Errno 17] File exists: 'C:\\Python\\PatientRecords1.csv'
但我在目录中没有看到该文件。即使将隐藏文件设置为 true 后也是如此。所以我运行了这个脚本。
from pathlib import Path
config = Path(filepath_o )
if config.is_file():
print('yes')
print(config)
else:
print('no')
得到这个输出,但是目录中没有文件!!困惑。
yes
C:\Python\PatientRecords1.csv
Extension 2
重写脚本以尝试使用目录
with open(filepath_i,'r') as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=',', quotechar='"')
with open('PatientRecords1.csv', 'w') as writeFile:
fieldnames = ['DRG Definition','Provider Id','Provider Name','Provider Street Address','Provider City','Provider State','Provider Zip Code','Hospital Referral Region Description','Hospital Category','Hospital Type', 'Total Discharges' ,'Covered Charges' , 'Total Payments' ,'Medicare Payments']
writer = csv.DictWriter(writeFile,fieldnames=fieldnames)
for row in csv_reader:
row['Provider Street Address'] = row['Provider Street Address'].replace("\n","").replace("\r","")
writer.writerows(row)
但收到此错误
Traceback (most recent call last):
File "CleanCSV.py", line 36, in <module>
writer.writerows(row)
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\csv.py", line 158, in writerows
return self.writer.writerows(map(self._dict_to_list, rowdicts))
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\csv.py", line 148, in _dict_to_list
wrong_fields = rowdict.keys() - self.fieldnames
AttributeError: 'str' object has no attribute 'keys'
示例输入文件数据
DRG Definition,Provider Id,Provider Name,Provider Street Address,Provider City,Provider State,Provider Zip Code,Hospital Referral Region Description,Hospital Category,Hospital Type, Total Discharges ,Covered Charges , Total Payments ,Medicare Payments
039 - EXTRACRANIAL PROCEDURES W/O CC/MCC,10001,SOUTHEAST ALABAMA MEDICAL CENTER,1108 ROSS CLARK CIRCLE,DOTHAN,AL,36301,AL - Dothan,Specialty Centers,Government Funded,91,"$32,963.07 ","$5,777.24 ","$4,763.73 "
039 - EXTRACRANIAL PROCEDURES W/O CC/MCC,10005,MARSHALL MEDICAL CENTER SOUTH,"2505 U S HIGHWAY
431 NORTH",BOAZ,AL,35957,AL - Birmingham,Specialty Centers,Private Institution,14,"$15,131.85 ","$5,787.57 ","$4,976.71 "
039 - EXTRACRANIAL PROCEDURES W/O CC/MCC,10006,ELIZA COFFEE MEMORIAL HOSPITAL,205 MARENGO STREET,FLORENCE,AL,35631,AL - Birmingham,Rehabilitation Centers,Private Institution,24,"$37,560.37 ","$5,434.95 ","$4,453.79 "
Extension 3
看起来文件是在目录中创建的,我得到了这两段代码的输出。但是我看不到该文件,想知道为什么!
with open(filepath_o,'r') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"')
for row in csv_reader:
print(row)
import os.path
from os import path
print(path.exists(filepath_o ))
最佳答案
转到 python 脚本所在的位置。单击文件夹的地址栏并输入 cmd
然后将从脚本的文件夹位置启动命令提示符
然后输入cmd
python CleanCSV.py
注意:您需要将 python 添加到环境变量中。
<小时/>如果您使用的是 Anaconda,请按照上述 Anaconda Prompt 中提到的步骤进行操作。
关于从命令行运行时Python不创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57326097/
我是一名优秀的程序员,十分优秀!