Python Forum
Saving data from each row into separate txt
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving data from each row into separate txt
#3
Dear wavic, I am thankful for the code you provided above. It worked fine for me during tests in the following form:

with open('file.csv', 'r') as in_file: #note it's a CSV
   while in_file.readline().split()[0] != 'FIPS':
       pass
    
   # write the rest in separed files
   for line in in_file.readline():
       with open('{}.txt'.format(line.slit()[0]), 'w') as out_file:
           out_file.write(line)
However, for some reason today it resulted in the following error:

Error:
--------------------------------------------------------------------------- IndexError                                Traceback (most recent call last) <ipython-input-61-11e3906ccb41> in <module>()      1 #pass all lines untils 'fips       txt'      2 with open('TW_13.csv', 'r') as in_file: ----> 3     while in_file.readline().split()[0] != 'FIPS':      4         pass      5 IndexError: list index out of range
The csv looks like this:

FIPS text
0 1001 I screwed that last tweet upAnd im ready to get...
1 1003 Like, please, we're all dying to know how many...
2 1005 ": å???: I haven't felt this good in a while ?...
3 1007 I always do ya favored. That's romance no hope...
4 1009 ???????????????? never heard it put that way...
... ... ...
3224 72153 Good morning gangstass :O å??å« Just is... PE...
3225 78010 lol, I know but I hear it has a dance??She do...
3226 78020 it's on a camera other than an iPhoneeeee14, ...
3227 78030 I'm gonna meet BeyoncÌ?å© after the show or im...
3228 rows × 2 columns
Could you please help me out with this. Thankfully, kiton
Reply


Messages In This Thread
RE: Saving data from each row into separate txt - by kiton - Jul-04-2017, 06:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Saving data into xlxs colomwise erdemath 0 1,458 Oct-02-2021, 09:01 AM
Last Post: erdemath
  Data saving structure JosefFilosopio 0 2,171 May-04-2019, 10:48 AM
Last Post: JosefFilosopio
  Problem with saving data and loading data to mysql kirito85 4 4,025 Feb-08-2019, 10:53 AM
Last Post: kirito85
  Saving data into .mat (Matlab Fiile) Tulo_Stone 0 2,442 Feb-28-2018, 12:57 AM
Last Post: Tulo_Stone

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020