G********7 发帖数: 234 | 1 要把在text里的数据转到csv格式, 程序如下。第四步(# Text file to CSV)出错。
出错提示是
“Traceback (most recent call last):
File "C:\yw\LatLon\txt2csv.py", line 24, in
txt = f.readline()
ValueError: Mixing iteration and read methods would lose data”
望赐教。多谢!
***********************************************
# Import Modules
import os,sys
# Variables
path = os.getcwd()
inputFile = os.path.join(path, "raw_files", "JAN 2013
FLVERTEXFLVertexFLVertex.txt")
CSVfile = os.path.join(path, "temp", "LatLon.csv")
print inputFile,CSVfile
# Write column names into CSV
outputCSV = open(CSVfile, 'w')
outputCSV.write("ID"+","+"LAT"+"'"+"LON"+"\n")
print "LatLon.csv is created"
# Text file to CSV
f = open(inputFile, 'r')
for lines in f:
txt = f.readline()
ID = txt [0:6]
LAT = txt [6:16]
LON = txt [16:27]
outputCSV.write (ID +"," + LAT + "," + LON + "," + "\n")
f.close
outputCSV.close | r*********r 发帖数: 3195 | 2 for lines in f:
txt = f.readline() | f**f 发帖数: 30 | 3 for 里边的lines 不是已经是每一行的string了吗,可以直接slice这个lines吧,为什
么还要读行f.readline?
【在 G********7 的大作中提到】 : 要把在text里的数据转到csv格式, 程序如下。第四步(# Text file to CSV)出错。 : 出错提示是 : “Traceback (most recent call last): : File "C:\yw\LatLon\txt2csv.py", line 24, in : txt = f.readline() : ValueError: Mixing iteration and read methods would lose data” : 望赐教。多谢! : *********************************************** : # Import Modules : import os,sys
| l********a 发帖数: 1154 | 4
+1
python和c++看着有一样的函数,可是那个for .. in .. 默认会帮你调用那个东西的,改
成下面就好了应该
# Text file to CSV
for lines in open(inputFile, 'r'):
ID = lines[0:6]
LAT = lines[6:16]
LON = lines[16:27]
outputCSV.write (ID +"," + LAT + "," + LON + "," + "\n")
【在 f**f 的大作中提到】 : for 里边的lines 不是已经是每一行的string了吗,可以直接slice这个lines吧,为什 : 么还要读行f.readline?
| G********7 发帖数: 234 | 5 谢谢大家。改了之后能转到csv了。可是原本是数字小数点后面的00都没了。打开csv文
件后出现如下提示 "The file you are trying to open ..is in a different format
than specified by the file extension...." | d********g 发帖数: 10550 | 6 csv你直接','.join()不就行了,搞这么多干啥
format
【在 G********7 的大作中提到】 : 谢谢大家。改了之后能转到csv了。可是原本是数字小数点后面的00都没了。打开csv文 : 件后出现如下提示 "The file you are trying to open ..is in a different format : than specified by the file extension...."
| d********g 发帖数: 10550 | 7 import os
import sys
path = os.getcwd()
input_file = os.path.join(path, 'raw_files', 'JAN 2013
FLVERTEXFLVertexFLVertex.txt')
output_file = os.path.join(path, 'temp', 'LatLon.csv')
with open(input_file, 'r') as fi:
with open(output_file, 'w') as fo:
output_file.write('ID,LAT,LON\n')
while True:
line = f.readline()
if not line:
break
id = line[0:6]
lat = line[6:16]
lon = line[16:27]
fo.write('%s\n' % ','.join([id, lat, lon]))
【在 d********g 的大作中提到】 : csv你直接','.join()不就行了,搞这么多干啥 : : format
| p**o 发帖数: 3409 | 8 open(outfile,'w').write(open(infile).read().replace('|', ',')) | c*****m 发帖数: 1160 | 9
用excel打开这个 .txt文档,然后它会问你怎么转换,你选择那个 | 号就可以按它来
分割每个field,然后你可以存成csv,也可以存成excel文档。
不需要编程。
【在 G********7 的大作中提到】 : 要把在text里的数据转到csv格式, 程序如下。第四步(# Text file to CSV)出错。 : 出错提示是 : “Traceback (most recent call last): : File "C:\yw\LatLon\txt2csv.py", line 24, in : txt = f.readline() : ValueError: Mixing iteration and read methods would lose data” : 望赐教。多谢! : *********************************************** : # Import Modules : import os,sys
|
|