![](/img/trans.png)
[英]Python+Django : I cannot see my rendered data in checkout but I am able to see it in other pages
[英]Cannot write my data to a file? I cannot see the error
我已经尝试了最近几个小时来缩小问题范围,但看不到它。 我是Python 3的新手,正在尝试解析项目的文本文件。 解析只是清除了一些空白并替换了定界符。 我不明白为什么它不起作用。
更具体地说,我收到此特定错误:
"NameError: name 'out' is not defined"
码:
save_path = 'C:/UsersDesktop/CSVproject'
with open('C:/Users/CSVproject/sourceData.dat', 'r') as f:
for line in f:
if ':DUBLIN' in line:
line = line.replace(' ', '')
line = line.replace(':', ';')
print(line)
found = True
fullNameOfFile = os.path.join(save_path, 'newFormattedData'+".csv")
out = open(fullNameOfFile, 'w')
for line in f:
out.write(line)
您尝试每次出现:DUBLIN单词时打开文件。 您只需要打开一次它,就应该在范围确保该句柄对write方法可见的地方打开它。
fullNameOfFile = os.path.join(save_path, 'newFormattedData'+".csv")
out = open(fullNameOfFile, 'w')
for line in f:
if ':DUBLIN' in line:
line = line.replace(' ', '')
line = line.replace(':', ';')
print(line)
found = True
out.write(line)
而且您绝对不希望有嵌套循环来遍历输入文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.