由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问一个python问题
相关主题
问个用python scratch yelp html 数据的问题Python Browsermob Proxy Library on mac issue
why I can not import Tkinter?请教exception handling的python问题
超级新手, 求助 python pandas 和pandas_DataReader问一个Mandriva 2007 下Tix的问题
咋用python做一个3D surface plot? 请教请问有没有用过IMSL库的大虾? (转载)
Usage of Grep???help!!!Help: undefined symbol
C++ 读不规则长度文件问题repast 请进: python debian安装问题
脚本问题求教谁来说一下这到底是C++还是C#的code ? thanks
怎样能把go写的稍微漂亮一点?问python高手一个问题
相关话题的讨论汇总
话题: lon话题: lat话题: csv话题: txt话题: file
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
问python高手一个问题Usage of Grep???help!!!
What's the problem with 'ascii' codec? (转载)C++ 读不规则长度文件问题
boost.python 入门问题脚本问题求教
python smtp 587 连不上gmail, socket error?怎样能把go写的稍微漂亮一点?
问个用python scratch yelp html 数据的问题Python Browsermob Proxy Library on mac issue
why I can not import Tkinter?请教exception handling的python问题
超级新手, 求助 python pandas 和pandas_DataReader问一个Mandriva 2007 下Tix的问题
咋用python做一个3D surface plot? 请教请问有没有用过IMSL库的大虾? (转载)
相关话题的讨论汇总
话题: lon话题: lat话题: csv话题: txt话题: file